Advanced routing
Edit this page on GitHubRest parameterspermalink
If the number of route segments is unknown, you can use rest syntax ā for example you might implement GitHub's file viewer like so...
/[org]/[repo]/tree/[branch]/[...file]
...in which case a request for /sveltejs/kit/tree/master/documentation/docs/04-advanced-routing.md
would result in the following parameters being available to the page:
ts
{org: 'sveltejs',repo : 'kit',branch : 'master',file : 'documentation/docs/04-advanced-routing.md'}
src/routes/a/[...rest]/z/+page.svelte
will match/a/z
(i.e. there's no parameter at all) as well as/a/b/z
and/a/b/c/z
and so on. Make sure you check that the value of the rest parameter is valid, for example using a matcher.
404 pagespermalink
Rest parameters also allow you to render custom 404s. Given these routes...
src/routes/
ā marx-brothers/
ā ā chico/
ā ā harpo/
ā ā groucho/
ā ā +error.svelte
ā +error.svelte
...the marx-brothers/+error.svelte
file will not be rendered if you visit /marx-brothers/karl
, because no route was matched. If you want to render the nested error page, you should create a route that matches any /marx-brothers/*
request, and return a 404 from it:
src/routes/
ā marx-brothers/
| ā [...path]/
ā ā chico/
ā ā harpo/
ā ā groucho/
ā ā +error.svelte
ā +error.svelte
ts
import {error } from '@sveltejs/kit';/** @type {import('./$types').PageLoad} */export functionload (event ) {throwerror (404, 'Not Found');}
ts
import {error } from '@sveltejs/kit';import type {PageLoad } from './$types';export constload = ((event ) => {throwerror (404, 'Not Found');}) satisfiesPageLoad ;
If you don't handle 404 cases, they will appear in
handleError
Optional parameterspermalink
A route like [lang]/home
contains a parameter named lang
which is required. Sometimes it's beneficial to make these parameters optional, so that in this example both home
and en/home
point to the same page. You can do that by wrapping the parameter in another bracket pair: [[lang]]/home
Note that an optional route parameter cannot follow a rest parameter ([...rest]/[[optional]]
), since parameters are matched 'greedily' and the optional parameter would always be unused.
Matchingpermalink
A route like src/routes/archive/[page]
would match /archive/3
, but it would also match /archive/potato
. We don't want that. You can ensure that route parameters are well-formed by adding a matcher ā which takes the parameter string ("3"
or "potato"
) and returns true
if it is valid ā to your params
directory...
ts
export functionmatch (param ) {return /^\d+$/.test (param );}
ts
export constmatch = ((param ) => {return /^\d+$/.test (param );}) satisfiesParamMatcher ;
...and augmenting your routes:
src/routes/archive/[page]
src/routes/archive/[page=integer]
If the pathname doesn't match, SvelteKit will try to match other routes (using the sort order specified below), before eventually returning a 404.
Matchers run both on the server and in the browser.
Sortingpermalink
It's possible for multiple routes to match a given path. For example each of these routes would match /foo-abc
:
src/routes/[...catchall]/+page.svelte
src/routes/[[a]]/foo/+page.svelte
src/routes/[b]/+page.svelte
src/routes/foo-[c]/+page.svelte
src/routes/foo-abc/+page.svelte
SvelteKit needs to know which route is being requested. To do so, it sorts them according to the following rules...
- More specific routes are higher priority (e.g. a route with no parameters is more specific than a route with one dynamic parameter, and so on)
- Parameters with matchers (
[name=type]
) are higher priority than those without ([name]
) [[optional]]
and[...rest]
parameters are ignored unless they are the final part of the route, in which case they are treated with lowest priority. In other wordsx/[[y]]/z
is treated equivalently tox/z
for the purposes of sorting- Ties are resolved alphabetically
...resulting in this ordering, meaning that /foo-abc
will invoke src/routes/foo-abc/+page.svelte
, and /foo-def
will invoke src/routes/foo-[c]/+page.svelte
rather than less specific routes:
src/routes/foo-abc/+page.svelte
src/routes/foo-[c]/+page.svelte
src/routes/[[a]]/foo/+page.svelte
src/routes/[b]/+page.svelte
src/routes/[...catchall]/+page.svelte
Encodingpermalink
Some characters can't be used on the filesystem ā /
on Linux and Mac, \ / : * ? " < > |
on Windows. The #
and %
characters have special meaning in URLs, and the [ ] ( )
characters have special meaning to SvelteKit, so these also can't be used directly as part of your route.
To use these characters in your routes, you can use hexadecimal escape sequences, which have the format [x+nn]
where nn
is a hexadecimal character code:
\
ā[x+5c]
/
ā[x+2f]
:
ā[x+3a]
*
ā[x+2a]
?
ā[x+3f]
"
ā[x+22]
<
ā[x+3c]
>
ā[x+3e]
|
ā[x+7c]
#
ā[x+23]
%
ā[x+25]
[
ā[x+5b]
]
ā[x+5d]
(
ā[x+28]
)
ā[x+29]
For example, to create a /smileys/:-)
route, you would create a src/routes/smileys/[x+3a]-[x+29]/+page.svelte
file.
You can determine the hexadecimal code for a character with JavaScript:
ts
':'.charCodeAt (0).toString (16); // '3a', hence '[x+3a]'
You can also use Unicode escape sequences. Generally you won't need to as you can use the unencoded character directly, but if ā for some reason ā you can't have a filename with an emoji in it, for example, then you can use the escaped characters. In other words, these are equivalent:
src/routes/[u+d83e][u+dd2a]/+page.svelte
src/routes/š¤Ŗ/+page.svelte
The format for a Unicode escape sequence is [u+nnnn]
where nnnn
is a valid value between 0000
and 10ffff
. (Unlike JavaScript string escaping, there's no need to use surrogate pairs to represent code points above ffff
.) To learn more about Unicode encodings, consult Programming with Unicode.
Since TypeScript struggles with directories with a leading
.
character, you may find it useful to encode these characters when creating e.g..well-known
routes:src/routes/[x+2e]well-known/...
Advanced layoutspermalink
By default, the layout hierarchy mirrors the route hierarchy. In some cases, that might not be what you want.
(group)permalink
Perhaps you have some routes that are 'app' routes that should have one layout (e.g. /dashboard
or /item
), and others that are 'marketing' routes that should have a different layout (/blog
or /testimonials
). We can group these routes with a directory whose name is wrapped in parentheses ā unlike normal directories, (app)
and (marketing)
do not affect the URL pathname of the routes inside them:
src/routes/
ā (app)/
ā ā dashboard/
ā ā item/
ā ā +layout.svelte
ā (marketing)/
ā ā about/
ā ā testimonials/
ā ā +layout.svelte
ā admin/
ā +layout.svelte
You can also put a +page
directly inside a (group)
, for example if /
should be an (app)
or a (marketing)
page.
Breaking out of layoutspermalink
The root layout applies to every page of your app ā if omitted, it defaults to <slot />
. If you want some pages to have a different layout hierarchy than the rest, then you can put your entire app inside one or more groups except the routes that should not inherit the common layouts.
In the example above, the /admin
route does not inherit either the (app)
or (marketing)
layouts.
+page@permalink
Pages can break out of the current layout hierarchy on a route-by-route basis. Suppose we have an /item/[id]/embed
route inside the (app)
group from the previous example:
src/routes/
ā (app)/
ā ā item/
ā ā ā [id]/
ā ā ā ā embed/
ā ā ā ā ā +page.svelte
ā ā ā ā +layout.svelte
ā ā ā +layout.svelte
ā ā +layout.svelte
ā +layout.svelte
Ordinarily, this would inherit the root layout, the (app)
layout, the item
layout and the [id]
layout. We can reset to one of those layouts by appending @
followed by the segment name ā or, for the root layout, the empty string. In this example, we can choose from the following options:
+page@[id].svelte
- inherits fromsrc/routes/(app)/item/[id]/+layout.svelte
+page@item.svelte
- inherits fromsrc/routes/(app)/item/+layout.svelte
+page@(app).svelte
- inherits fromsrc/routes/(app)/+layout.svelte
+page@.svelte
- inherits fromsrc/routes/+layout.svelte
src/routes/
ā (app)/
ā ā item/
ā ā ā [id]/
ā ā ā ā embed/
ā ā ā ā ā +page@(app).svelte
ā ā ā ā +layout.svelte
ā ā ā +layout.svelte
ā ā +layout.svelte
ā +layout.svelte
+layout@permalink
Like pages, layouts can themselves break out of their parent layout hierarchy, using the same technique. For example, a +layout@.svelte
component would reset the hierarchy for all its child routes.
src/routes/
ā (app)/
ā ā item/
ā ā ā [id]/
ā ā ā ā embed/
ā ā ā ā ā +page.svelte // uses (app)/item/[id]/+layout.svelte
ā ā ā ā +layout.svelte // inherits from (app)/item/+layout@.svelte
ā ā ā ā +page.svelte // uses (app)/item/+layout@.svelte
ā ā ā +layout@.svelte // inherits from root layout, skipping (app)/+layout.svelte
ā ā +layout.svelte
ā +layout.svelte
When to use layout groupspermalink
Not all use cases are suited for layout grouping, nor should you feel compelled to use them. It might be that your use case would result in complex (group)
nesting, or that you don't want to introduce a (group)
for a single outlier. It's perfectly fine to use other means such as composition (reusable load
functions or Svelte components) or if-statements to achieve what you want. The following example shows a layout that rewinds to the root layout and reuses components and functions that other layouts can also use:
<script>
import ReusableLayout from '$lib/ReusableLayout.svelte';
export let data;
</script>
<ReusableLayout {data}>
<slot />
</ReusableLayout>
ts
import {reusableLoad } from '$lib/reusable-load-function';/** @type {import('./$types').PageLoad} */export functionload (event ) {// Add additional logic here, if neededreturnreusableLoad (event );}
ts
import {reusableLoad } from '$lib/reusable-load-function';import type {PageLoad } from './$types';export constload = ((event ) => {// Add additional logic here, if neededreturnreusableLoad (event );}) satisfiesPageLoad ;