svelte5-router is designed to work in server-side rendering (SSR) environments such as SvelteKit or a custom Node.js server. The key is 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.
url prop on <Router>: when it receives a truthy string, the router ignores window.location and uses that string as the current pathname instead, allowing routes to be matched synchronously during server rendering before any DOM exists.
How SSR Routing Works
On the server, there is nowindow or browser history. svelte5-router detects this with typeof window === "undefined" and switches to a synchronous registration path:
- Each
<Route>registers itself immediately when the component initialises (noonMount). - As soon as a route is registered that matches
url, the router setshasActiveRoute = trueand ignores all subsequent registrations. - The rendered HTML contains only the markup for the matching route, ready to hydrate on the client.
url (the "" default) is silently ignored and window.history is used as normal.
SSR sets the active route synchronously via a
hasActiveRoute flag. The first matching route wins, so the order of <Route> declarations can matter in SSR if two routes could match the same URL — give more specific routes a more specific path to ensure the right one fires first.The url Prop Pattern
The recommended approach is to accept url as an optional prop in your top-level component. In the browser the prop is omitted so url defaults to "" (falsy and ignored). On the server, the parent or entry point passes the real request pathname:
App.svelte
SvelteKit Integration
SvelteKit renders each page on the server before sending it to the browser. Pass the request URL from the server-sideload function into your App component via props.
Create a layout or page component
Use SvelteKit’s
$page store (or a load function) to obtain the current pathname and forward it to your router.+page.svelte
Use a load function for full control
Alternatively, return the URL from a
+page.server.ts load function and receive it as a prop:+page.server.ts
+page.svelte
Custom Node.js Server Example
If you are not using SvelteKit, you can render the app to HTML with Svelte’srender utility and pass the request URL directly:
Hydration
After the server sends the HTML, Svelte hydrates the page in the browser. Becauseurl defaults to "" (falsy), the Router ignores it and uses window.location to determine the active route. The hydrated state matches the server-rendered HTML, so no layout shift occurs.
Router Internals: SSR vs Browser
| Step | Browser | Server |
|---|---|---|
| Location source | window.history | url prop |
| Route registration | onMount (async) | Synchronous, immediate |
| Stops after first match? | No — all routes register | Yes — hasActiveRoute flag |
| Scroll reset | Yes, via window.scrollTo | Skipped (canUseDOM() returns false) |