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 |
- A folder's name becomes part of the URL: the
menufolder serves/menu. The top folder,src/routesitself, serves/. - A page has to be named exactly
+page.svelte. The+marks a file that belongs to SvelteKit. An ordinary file such aspage.svelteorCard.sveltein the same folder is ignored by the router, so the address stays 404. - A wrong letter in a
+file is worse:+Page.sveltemakes SvelteKit stop withFiles prefixed with + are reserved (saw src/routes/promo/+Page.svelte).
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()}
+layout.sveltewraps the pages in its folder and every subfolder below it. This header shows on/,/menuand/menu/2.- SvelteKit sends the page to the layout as the
childrenprop, a snippet, just likechildrenin the Slots lesson.{@render children()}is where the page is drawn. Forget it, and every page only shows its header; the page's content doesn't appear at all. - Layouts can be nested.
menu/+layout.sveltealso takeschildrenfrom$props(), then writes<section class="menu"><p>Order before 14.00</p>{@render children()}</section>, so/menu/2is drawn as the header from the top layout, then the section from the menu layout, then the page./doesn't get the menu layout.
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.
- SvelteKit's template puts
data-sveltekit-preload-data="hover"on<body>, so SvelteKit starts preparing the target page as soon as the pointer rests on its link. - Write addresses with a leading slash,
/menu. From/menu/2,href="menu"is resolved relatively and becomes/menu/menu, which actually matches the[id]folder and showsDish number menu.
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>
- On
/menu/2,page.params.idholds the string"2", not the number 2. The name inside the brackets becomes the key:[slug]givespage.params.slug. pagefrom$app/stateis an ordinary reactive object, so it's written without a$. It also haspage.url.pathname, the address that's open, such as"/menu/2".$app/stateexists since SvelteKit 2.12.- Finding the data for dish number 2 is the job of the
loadfunction, in the next lesson.
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 />
<slot />becomes{@render children()}, withlet { children } = $props(). In a runes component<slot />still works, but the compiler warnsUsing `<slot>` to render parent content is deprecated. Use `{@render ...}` tags instead.$pagefrom$app/storesbecomespagefrom$app/state, without the$. SvelteKit 3 has removed$app/stores, and the error reads$app/stores has been removed in favour of $app/state.
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
- A misnamed file.
page.sveltewithout the+is ignored (404),+Page.sveltemakes SvelteKit stop. Write exactly+page.svelteand+layout.svelte. - A layout without
{@render children()}. Every page below it shows only the layout. - An address without a leading slash.
href="menu"is resolved from the current address. Usehref="/menu". - What the exercises check. The first: there's a
<nav>, at least 2 links with anhref, one to/and one to/about, and your code renderschildrenwith{@render children?.()}. The second: the<h1>readsHome, at least 3 links, and the nav's first, second and third links go to/,/blog,/about, with the labelBlogwritten in. Wrapping each link in an<li>makes that first, second, third order unreadable.
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.