MCPcopy Index your code
hub / github.com/WJSoftware/svelte-router

github.com/WJSoftware/svelte-router @v1.0.7

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.7 ↗ · + Follow
206 symbols 654 edges 79 files 26 documented · 13%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Svelte Router Logo Svelte Router

Next-level routing for Svelte and Sveltekit.

REPL Demo

Full Documentation

Features

  • Always-on path and hash routing: Simultaneous, independent and always-on routing modes.
  • Multi hash routing: Doing micro-frontends? Routing tabs or dialogs using the URL? Have as many paths as needed.
  • Sveltekit support: Add hash routing on top of Sveltekit's path routing via @svelte-router/kit
  • Electron support: Works with Electron (all routing modes)
  • Reactivity-based: All data is reactive, reducing the need for events and imperative programming.
  • URL Redirection: Use Redirector instances to route users from deprecated URL's to new URL's, even across routing universes.
  • Dynamic Routes: Define routes in JavaScript from any dynamic source, even fetched data.
  • Fully Typed: Built-in TypeScript, even for route parameters.

Components:

  • <Router>
  • <Route>
  • <Fallback>
  • <Link>
  • <LinkContext>
  • <RouterTrace>

Reactive Data:

  • location.url
  • location.path
  • location.hashPaths
  • location.getState()
  • RouterEngine.routes
  • RouterEngine.routeStatus
  • RouterEngine.basePath

All data is a Svelte signal. Add routes dynamically or reactively, change route conditions on the fly, add more pieces of user interface on-demand, etc. All works reactively.

Two Library Modes

Most people only need the normal or "lite" version. Use the full version to battle/counter foreign routers (micro-frontend scenarios, most likely).

In Full Mode...

  • History API interception: Gain control over the history object to avoid external code/routers from de-synchronizing state.
  • Cancellable beforeNavigate event: Get notified of navigation events, and cancel when appropriate.
  • navigationCancelled event: Get notified whenever navigation is cancelled.

Quickstart

  1. Install the package.
  2. Initialize the library.
  3. Define the routes inside routers.
  4. Modify/Add your navigation links.

Install the package

npm i @svelte-router/core

Initialize the Library

// In your main.ts, or somewhere BEFORE any routers are created:
import { init } from '@svelte-router/core';

/*
Default:

- Lite mode
- Implicit path routing
- No router hierarchy tracing
- Single hash mode
- Log to console.
*/
init(); // Or use initFull() for full-mode.

// Common case:  "I just need good, old-fashioned hash routing."
init({ defaultHash: true });

Electron Variant

In Electron, perform immediate navigation to clean the environment's path:

import { init, location } from '@svelte-router/core';

init();
location.goTo('/');

⚠️ Important: Hash routing doesn't require this extra navigation step.

For applications that also run in the browser, condition the navigation to Electron only. See the Electron page online for more details.

Define the Routes

<script lang="ts">
    import { Router, Route } from '@svelte-router/core';
    import NavBar from './lib/NavBar.svelte';
    import UserView from './lib/UserView.svelte';
</script>

<Router>
    <NavBar />




        <h1>Routing Demo</h1>
        <Route key="users" path="/users">

        </Route>
        <Route key="user" path="/users/:userId">

            {#snippet children({ rp })}
                <UserView id={rp?.userId} /> 
            {/snippet}
        </Route>
        ...



</Router>

Navigation Links

The best practice is to render the links inside a router's hierarchy, but this is not mandatory.


<script lang="ts">
    import { Link } from '@svelte-router/core';
</script>

<nav>



        <ul>
            <li class="nav-link">
                <Link href="https://github.com/WJSoftware/svelte-router/raw/v1.0.7/users" activeFor="users" activeState={{ class: 'active' }}
                    >All Users</Link
                >
            </li>
            ...
        </ul>



</nav>

Issues Here

Questions, Polls, Show & Tell, etc. Here

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Function 117
Method 63
Class 22
Interface 4

Languages

TypeScript100%

Modules by API surface

src/lib/testing/test-utils.ts27 symbols
src/lib/types.ts14 symbols
src/lib/kernel/LocationLite.svelte.ts14 symbols
src/lib/kernel/RouterEngine.svelte.ts13 symbols
src/lib/kernel/StockHistoryApi.svelte.ts12 symbols
src/lib/Link/Link.svelte.test.ts11 symbols
src/lib/Router/Router.svelte.test.ts10 symbols
src/lib/Route/Route.svelte.test.ts10 symbols
src/lib/kernel/InterceptedHistoryApi.svelte.ts8 symbols
src/lib/kernel/RouteHelper.svelte.ts7 symbols
playground/src/lib/state/title.svelte.ts6 symbols
playground/src/lib/state/theme.svelte.ts6 symbols

For agents

$ claude mcp add svelte-router \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page