Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 35 ยท SvelteKit

SvelteKit Routing and Layouts

Learn SvelteKit file-based routing - +page.svelte, +layout.svelte, dynamic [param] routes, optional and rest params, route groups, error pages and navigation.

Svelte builds components. SvelteKit is the application framework around it: routing, server rendering, data loading, forms, deployment. If Svelte is the engine, SvelteKit is the car. The first thing you meet is its file-system router: the folders under src/routes are your URLs.

SvelteKit needs Node, a bundler and a server, none of which run in your browser here. All SvelteKit code in this chapter is read-only; the <TryIt> blocks are stand-ins in plain JavaScript that model how SvelteKit decides things, such as which file matches a URL.

Creating a project

Terminal
npx sv create my-app
cd my-app
npm install
npm run dev

The sv tool asks which options you want (TypeScript, ESLint, Vitest, Playwright, an adapter) and scaffolds a project. The important folders:

TEXT
src/
  routes/          the router: folders become URLs
    +layout.svelte
    +page.svelte
  lib/             your shared code, imported as $lib/...
  app.html         the HTML shell
static/            files served as-is (favicon, robots.txt)
svelte.config.js   adapter and preprocess settings

Pages and layouts

A folder in src/routes becomes a URL segment, and a file named +page.svelte inside it is the page for that URL:

File URL
src/routes/+page.svelte /
src/routes/about/+page.svelte /about
src/routes/blog/+page.svelte /blog
src/routes/blog/first-post/+page.svelte /blog/first-post

Files starting with + are special SvelteKit files; anything else in the folder (a Card.svelte, a helper) is ordinary code that lives next to the route. A page is a normal component:

HTML
<!-- src/routes/about/+page.svelte -->
<svelte:head>
  <title>About us</title>
</svelte:head>

<h1>About</h1>
<p>We write tutorials.</p>

A +layout.svelte wraps every page in its folder and below. It receives the page through the children snippet:

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

<nav>
  <a href="/">Home</a>
  <a href="/about">About</a>
  <a href="/blog">Blog</a>
</nav>

<main>
  {@render children()}
</main>

Layouts nest: src/routes/blog/+layout.svelte wraps blog pages inside the root layout. The navigation bar stays mounted while you move between pages, so its state is kept. Links are plain <a href> elements; SvelteKit intercepts clicks and navigates on the client without a full page load.

Dynamic routes

A folder name in square brackets is a parameter:

TEXT
src/routes/blog/[slug]/+page.svelte     /blog/hello  ->  slug = "hello"
src/routes/shop/[cat]/[id]/+page.svelte /shop/shoes/42

Other forms:

  • [[lang]] is an optional parameter: /about and /en/about both match [[lang]]/about.
  • [...path] is a rest parameter: it matches any number of segments, docs/[...path] matches /docs, /docs/a, /docs/a/b/c.
  • [id=integer] applies a matcher from src/params/integer.js, so /post/42 matches but /post/abc does not.

When several routes could match, SvelteKit prefers the most specific one: a static segment beats a parameter, a parameter beats a rest.

How matching works, as code

This stand-in turns route patterns into matchers and ranks them the way SvelteKit does:

JS A tiny file-system router

Press Run (or Ctrl + Enter) to see the console output here.

No output.

/blog/archive goes to the static page even though /blog/[slug] also matches. /shop/abc fails the integer matcher and results in a 404.

Route groups

A folder in parentheses is a group: it organizes files and layouts without adding a segment to the URL.

TEXT
src/routes/
  (marketing)/
    +layout.svelte      marketing layout (wide hero)
    about/+page.svelte  /about
  (app)/
    +layout.svelte      app layout (sidebar)
    dashboard/+page.svelte  /dashboard

Use groups when different parts of the site need different layouts. A page can also break out of a layout it would inherit: name the file [email protected] to reset to the root layout.

Errors and 404 pages

A +error.svelte file renders when load throws or no route matches. The page state tells you what happened:

HTML
<!-- src/routes/+error.svelte -->
<script>
  import { page } from '$app/state';
</script>

<h1>{page.status}</h1>
<p>{page.error?.message}</p>

Errors render inside the nearest layout above the error file, so your navigation still appears.

  • <a href="/blog"> is all most navigation needs. Add data-sveltekit-preload-data="hover" on <body> (the default template does) to start loading a page's data when the pointer hovers the link.
  • goto('/dashboard') from $app/navigation navigates from code, for example after a login.
  • page.url, page.params and page.route.id (from $app/state) are reactive values describing the current location. Use them to mark the active link in a nav.
HTML
<script>
  import { page } from '$app/state';
</script>

<a href="/blog" aria-current={page.url.pathname.startsWith('/blog') ? 'page' : undefined}>Blog</a>

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file makes /about a page in SvelteKit?

    A folder under src/routes provides the URL segment and +page.svelte inside it is the page component.
  2. 2. What does a +layout.svelte render its page with?

    In Svelte 5 the layout receives a children snippet through $props() and renders it with {@render children()}.
  3. 3. Which route folder matches /docs, /docs/a and /docs/a/b?

    [...path] is a rest parameter matching any number of segments. [path] needs exactly one; (path) is a group that does not affect the URL.
  4. 4. What does a folder named (marketing) do to the URL?

    Parentheses create a route group: shared layout, no URL segment.

Summary

  • Folders in src/routes are URLs; +page.svelte is the page, +layout.svelte wraps pages and nests, +error.svelte handles failures.
  • [param], [[optional]], [...rest] and [id=matcher] make dynamic routes; the most specific match wins.
  • (group) folders share layouts without changing the URL.
  • Use <a href>, goto and page from $app/state for navigation and URL information.

Next: Load Functions and Data Flow