TheDocumentation 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 is the backbone of every svelte5-router application. It must wrap all Link and Route descendants, because it exposes routing context (current location, active route, history) through Svelte’s context API. At mount time, Router collects every child Route, assigns each a numeric score based on its path pattern, and reactively picks the highest-scoring match whenever the URL changes. Routers can be freely nested: a child Router inherits the matched URI of the nearest ancestor Router as its own base, enabling seamless composition of independently-developed sub-applications.
Props
A prefix prepended to every descendant
Route path and Link to value. Use this when your application is mounted at a sub-path, such as https://example.com/my-site — in that case set basepath="/my-site". In most applications this prop can be omitted.Forces the current URL during server-side rendering (SSR). The
Router uses this string as the pathname instead of reading window.location. A falsy value (empty string, undefined) is silently ignored in the browser, so you can safely declare let url = $state("") in your top-level component and only populate it on the server.An experimental callback that enables animated route transitions powered by Svelte’s built-in
transition: directive. When provided, the active route content is wrapped in a keyed <div> that applies the returned Viewtransition object as both an in: and out: transition on every pathname change. See the Viewtransition type below for the full set of configurable fields.A custom history source that replaces the default browser history implementation. The object must expose the same interface as
globalHistory (a location property, a listen method, and a navigate method). Pass a custom history when writing tests or when embedding the router in environments without a real browser history.The content of the router — all
Route and Link components should live inside this snippet. The snippet receives two arguments: the URI of the currently active route (string | null) and the current RouteLocation object.TypeScript Types
RouterProps
Viewtransition type
The object returned by the viewtransition callback. If a fn field is provided, it is invoked as a Svelte transition function. Otherwise the remaining fields are passed directly to Svelte’s fly-style transition engine.
Route Scoring
When the URL changes,Router calls pick() against all registered child Route components. Each route receives a numeric score calculated segment-by-segment:
| Segment type | Points added |
|---|---|
| Any segment present | +4 |
Static segment (e.g. about) | +3 |
Dynamic parameter (e.g. :id) | +2 |
Splat wildcard (*) | −5 (4 + 1 penalty) |
Root / | +1 |
Default route (path="") | 0 |
<Route> components manually — <Route path="/blog/:id" /> will always beat <Route path="/blog" /> regardless of the order they appear in the template.
Nested Routers
When aRouter renders inside another Router, it reads the parent’s routerBase from context and uses it as its own base. The child router’s basepath prop is combined with the matched URI of the active route in the parent, so all descendant Link and Route components resolve paths relative to that combined base. This allows you to compose self-contained sub-applications without duplicating path prefixes.