SvelteKit Routing — Svelte

In SvelteKit, folders in src/routes are addresses: src/routes/+page.svelte serves /, menu/+page.svelte serves /menu, and the [id] folder is a dynamic route…

Mrs. Sri's stall site now needs several addresses: the home page at /, the menu list at /menu, and one page per dish at /menu/2. Svelte itself has no way to manage addresses. That's the job of SvelteKit, the official framework built on Svelte that adds routing, a server, and the build. Its routing is file-based: each folder inside src/routes is one URL address, and the +page.svelte file in that folder is the page for that address. If you've used Next.js or Nuxt, the idea is the same: folders become addresses.

Picture the floor plan of Mrs. Sri's stall. The front door opens onto the dining room, from there a "menu" corridor leads to the display case, and every plate in the case has a number. There's no separate list of directions. The floor plan itself is the directions. The roof and signboard cover every room, and the menu corridor has its own small canopy. The rooms are folders, the way there is the URL, the plate number is the [id] parameter, and the roof and canopy are layouts.

One folder, one address

A new SvelteKit project is made with npx sv create mrs-sri-stall, and it uses Svelte 5 with runes from the start. This is the stall project's src/routes folder:

src/routes/
├── +layout.svelte
├── +page.svelte
└── menu/
    ├── +layout.svelte
    ├── +page.svelte
    └── [id]/
        └── +page.svelte
Address Its page Wrapped in layouts
/ src/routes/+page.svelte +layout.svelte
/menu menu/+page.svelte +layout.svelte, then menu/+layout.svelte
/menu/2 menu/[id]/+page.svelte, with id = "2" same as /menu
/drinks there's no drinks folder the 404 error page

Layout: a frame for many pages

<!-- src/routes/+layout.svelte -->
<script>
  let { children } = $props()
</script>

<header>
  <h1>Mrs. Sri's Stall</h1>
  <nav>
    <a href="/">Home</a>
    <a href="/menu">Menu</a>
  </nav>
</header>

{@render children()}

Navigation: an ordinary link is enough

SvelteKit needs no special component for links. An ordinary <a href="/menu/2"> is enough: SvelteKit intercepts clicks to addresses in its own app, prepares the target page in the browser, then swaps only the page. The same layout stays mounted.

Tried in a browser: from /menu, click to /menu/2, then to /menu/3. The server receives no request for either page, and menu/+layout.svelte is mounted only once. The header isn't redrawn, and nothing reloads. A request to the server only happens when the address is typed into the address bar or the page is refreshed; the server then sends complete HTML.

Dynamic routes: [id]

A folder named in square brackets, such as [id], is a dynamic route: it matches anything in that position. /menu/1, /menu/2 and /menu/3 are served by the same file, and whatever is in that part is read as a parameter:

<!-- src/routes/menu/[id]/+page.svelte -->
<script>
  import { page } from "$app/state"
</script>

<h2>Dish number {page.params.id}</h2>

404: an address with no folder

/drinks has no folder, so SvelteKit draws its built-in error page inside the top layout: the header still shows, then <h1>404</h1> and <p>Not Found</p>. A custom error page, +error.svelte, comes with load.

Older code: slot and $app/stores

Older SvelteKit projects and tutorials write layouts with <slot />, and read the address through the $page store:

<script>
  import { page } from "$app/stores"
</script>

<!-- header and nav as above -->

<p>You are at {$page.url.pathname}</p>
<slot />

Exercise: layout and navigation

The first exercise is a +layout.svelte: the starter already has a <nav> with a Home link to / and an About link to /about, and you add let { children } = $props() in <script> and {@render children?.()} below the nav. Use ?.: the exercise editor sends no page to the layout, so children is empty, and children() without ?. stops with an error. Only the nav shows, and that's expected.

The second exercise is the home page: <h1>Home</h1>, then a <nav> holding three links in order, directly inside <nav> with no <ul> or <li>: <a href="/">Home</a>, <a href="/blog">Blog</a>, <a href="/about">About</a>.

⚠️ Why the address is a 404 or the page is empty

TL;DR: In SvelteKit, folders in src/routes are addresses: src/routes/+page.svelte serves /, menu/+page.svelte serves /menu, and the [id] folder is a dynamic route serving /menu/1, /menu/2 and so on, with page.params.id (from $app/state) holding a string such as "2". A page file has to be named exactly +page.svelte. +layout.svelte wraps every page in its folder and subfolders, with {@render children()} as the page's place; layouts can be nested and stay mounted when the page changes. Navigation only needs <a href="/menu">: SvelteKit swaps the page in the browser without reloading. An address with no folder shows a 404 error inside the layout. A new project is made with npx sv create. In the exercise, write {@render children?.()}, because the editor sends no page to the layout. The exercises: a nav with Home and About plus {@render children?.()}, then <h1>Home</h1> with three links to /, /blog and /about.

⚡ One folder, one address

Write:

src/routes/
├── +layout.svelte
├── +page.svelte
└── menu/
    ├── +layout.svelte
    ├── +page.svelte
    └── [id]/
        └── +page.svelte
Guess first: the page file is named page.svelte, without the +. Does the address work?

No. A file without the + is ignored by the router, so the address stays 404.

Meaning: Folders in src/routes are addresses: +page.svelte serves /, and menu/+page.svelte serves /menu. A page file has to be named exactly +page.svelte.

⚡ A layout wraps many pages

Write:

<!-- src/routes/+layout.svelte -->
<script>
  let { children } = $props()
</script>

<header>
  <h1>Mrs. Sri's Stall</h1>
  <nav>
    <a href="/">Home</a>
    <a href="/menu">Menu</a>
  </nav>
</header>

{@render children()}

Meaning: +layout.svelte wraps the pages in its folder and every subfolder below it. The page arrives as the children prop, and {@render children()} is where it's drawn. Moving between pages only needs an ordinary <a href="/menu">.

⚡ A dynamic route: [id]

Write:

<!-- src/routes/menu/[id]/+page.svelte -->
<script>
  import { page } from "$app/state"
</script>

<h2>Dish number {page.params.id}</h2>
Guess first: on /menu/2, does page.params.id hold the number 2?

Not a number, the string "2". The name inside the brackets becomes the key.

Meaning: A folder named in square brackets matches anything in that position, so /menu/1, /menu/2 and /menu/3 are served by the same file.

What you will learn