svelte5-router is a declarative routing library built for Svelte 5. You wrap your application in aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/jpcutshall/svelte5-router/llms.txt
Use this file to discover all available pages before exploring further.
<Router> component that acts as a context provider, declare <Route> components to map URL paths to views, and use <Link> components to navigate between them — no imperative configuration required.
Installation
Install the package as a dev dependency:Setting Up Your First Router
TheRouter component supplies routing context to all Link and Route descendants. Every Svelte 5 app needs at least one Router at the top of the component tree.
Import the core components
Bring in
Router, Route, and Link from svelte5-router, along with the page components you want to render.App.svelte
Declaring
let url = $state("") is the recommended pattern. A falsy value is ignored in the browser but lets you pass a real URL during SSR. See the SSR guide for details.Wrap your layout in Router
Pass the
url prop to Router. Inside it, add navigation links and your route declarations.App.svelte
Route Path Matching
Each<Route path="..."> is evaluated against the current URL’s pathname. The router scores every registered route and renders the highest-scoring match.
How Scoring Works
The router uses a segment-based scoring system so you never need to worry about declaration order. Every segment starts with a base of 4 points (SEGMENT_POINTS), then an additional bonus or penalty is applied based on the segment type:
| Segment type | Calculation | Score per segment |
|---|---|---|
Static (e.g. about) | 4 (SEGMENT_POINTS) + 3 (STATIC_POINTS) | 7 |
Dynamic (e.g. :id) | 4 (SEGMENT_POINTS) + 2 (DYNAMIC_POINTS) | 6 |
Root ("") | 4 (SEGMENT_POINTS) + 1 (ROOT_POINTS) | 5 |
Wildcard (*) | 4 (SEGMENT_POINTS) − 4 − 1 (SPLAT_PENALTY) | −1 |
static > dynamic > root > splat. A route like /blog/:id will always beat /blog/* for a URL like /blog/42.
The Default (Fallback) Route
A<Route> with no path prop (or path="") acts as the fallback that matches when no other route does. Use this for 404 pages or catch-all layouts.
Complete Working Example
The example below mirrors the structure used in the library’s own dev app, with Home, About, and Blog routes:Using the Link Component
<Link> renders a standard <a> element that pushes onto the browser history stack. It accepts all standard anchor attributes plus a few routing-specific ones.
| Prop | Default | Description |
|---|---|---|
to | "#" | The URL to navigate to |
replace | false | Replace the history entry instead of pushing a new one |
state | {} | Object pushed to the history state |
preserveScroll | false | Prevent the page from scrolling to the top after navigation |
Active Link State via the Children Snippet
Link exposes an active boolean to its children snippet. Use this to style the currently active link — for example, by passing it into a custom menu item component.
active value is true when the link’s href exactly matches the current pathname (aria-current="page" is also set automatically).
Rendering Children Directly in Route
Instead of passing acomponent prop, you can render children directly inside <Route>. This is useful when you need to pass explicit static props:
component prop when you want the router to spread URL parameters directly onto the component as props (covered in Dynamic Routes).