svelte5-router supports nestingDocumentation 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> components inside one another, which lets you break a large application into self-contained sub-apps with their own route trees. Each nested router inherits the routerBase of its parent so relative paths resolve correctly, and the basepath prop lets you scope an entire sub-tree under a URL prefix — useful when your app is deployed at a path other than the root.
Why Nest Routers?
Modular sub-apps
Each feature area (admin panel, user dashboard, docs) can own its router and route list without touching the top-level config.
Sub-path hosting
If your app lives at
https://example.com/my-site, set basepath="/my-site" on the root Router and all links and routes resolve correctly.Seamless context merging
Nested routers read the parent’s
routerBase through Svelte context, so they always know their effective base path without any manual wiring.Independent matching
Each router scores and picks its own best-matching route independently, so inner routers don’t interfere with outer ones.
The basepath Prop
Setting basepath on a <Router> prepends that prefix to every <Route path> and <Link to> value that is a descendant of that router. The default value is "/".
The routerBase Derived Store
Internally, each Router maintains a routerBase derived store that tracks the currently matched path and URI. When a Router is nested, it reads the routerBase of its parent from Svelte context and uses it as its own base. This means nested routers always stay in sync with their parent’s active route without any extra configuration.
You can access this store in your own code using the useRouter context hook:
Nesting Routers: Step by Step
Create a top-level Router
The root router handles your app’s main routes. Nest sub-apps as route children.
App.svelte
Complete Example: Parent at /app, Nested at /app/admin
The following example shows a parent router serving /app/* routes and a nested router scoped to /app/admin/*.
Hosting at a Sub-Path
If your application is deployed athttps://example.com/my-site instead of the root, set basepath on the topmost router:
App.svelte
The
basepath value is prepended to paths exactly as written. Make sure it matches the path prefix configured in your server or static file host. There is no trailing-slash normalisation, so /my-site and /my-site/ behave differently.Tips
Do I need to repeat the basepath in every Route path?
Do I need to repeat the basepath in every Route path?
Yes. Each
<Route path> value should be the full absolute path including the basepath prefix. The router does not strip the basepath automatically at runtime — it prepends the base to the path you give so they both point to the same URL.Can I nest more than two levels deep?
Can I nest more than two levels deep?
Yes. Routers can be nested to any depth. Each level inherits the
routerBase of its parent, so paths always compose correctly as long as you keep them absolute.Can the nested router use a different history source?
Can the nested router use a different history source?
Each
Router accepts an optional history prop. If you omit it, the nested router uses globalHistory just like the parent. Passing a custom history (e.g. a memory source) lets you isolate the sub-app’s navigation — useful for testing.