Skip to main content
At the heart of SvelteKit is a filesystem-based router. The routes of your app — the URL paths that users can access — are defined by the directories in your codebase.

Basic routing

Routes are created by adding files to the src/routes directory:
You can change src/routes to a different directory by editing the project configuration.

Route files

Each route directory contains one or more route files, which can be identified by their + prefix.

Key routing rules

Server execution

All files can run on the server

Client execution

All files run on the client except +server files

Inheritance

+layout and +error files apply to subdirectories

Page components

+page.svelte

A +page.svelte component defines a page of your app. By default, pages are rendered both on the server (SSR) for the initial request and in the browser (CSR) for subsequent navigation.
SvelteKit uses <a> elements to navigate between routes, rather than a framework-specific <Link> component.

Receiving data

Pages can receive data from load functions via the data prop:

Receiving params

As of SvelteKit 2.24, pages also receive a params prop typed based on the route parameters:

Load functions

+page.js

Often, a page will need to load some data before it can be rendered. Add a +page.js module that exports a load function:
This function runs alongside +page.svelte, which means it runs on the server during server-side rendering and in the browser during client-side navigation.

+page.server.js

If your load function can only run on the server — for example, if it needs to fetch data from a database or access private environment variables — rename +page.js to +page.server.js:
During client-side navigation, SvelteKit will load this data from the server, which means that the returned value must be serializable using devalue.

Layouts

+layout.svelte

To create a layout that applies to every page, make a file called src/routes/+layout.svelte:

Nested layouts

Layouts can be nested. For example, create a layout that only applies to pages below /settings:

+layout.js

Your +layout.svelte component can get data from a load function in +layout.js:
Data returned from a layout’s load function is available to all its child pages.

Server routes

You can define routes with a +server.js file (API routes), which gives you full control over the response:

HTTP methods

Your +server.js file can export functions corresponding to HTTP verbs:

Error handling

+error.svelte

If an error occurs during load, SvelteKit will render a default error page. Customize this error page on a per-route basis by adding an +error.svelte file:
SvelteKit will ‘walk up the tree’ looking for the closest error boundary. If no +error.svelte file exists, it will try parent directories before rendering the default error page.

Type safety

SvelteKit creates a $types.d.ts file for type safety when working with route files:
If you’re using VS Code or any IDE that supports the language server protocol and TypeScript plugins, you can omit these types entirely — Svelte’s IDE tooling will insert the correct types for you.

Next steps

Load functions

Learn how to load data for your pages

Form actions

Handle form submissions progressively