SvelteKit Routing
SvelteKit builds page addresses from your folder structure. A folder inside the routes directory becomes a URL segment, and special files inside it define what visitors see. The approach removes the need for a separate routing table.
File-Based Routes
Each folder under src/routes holds a file named +page.svelte. That file renders when a visitor opens the matching address.
src/routes/
+page.svelte --> /
about/
+page.svelte --> /about
blog/
+page.svelte --> /blog
[slug]/
+page.svelte --> /blog/any-post-nameThe plus sign prefix marks files that SvelteKit treats specially. Files without it, such as helper components, never become routes.
Route Map Diagram
Visitor opens /blog/learn-svelte
|
v
SvelteKit checks src/routes
|
v
Finds blog/[slug]/+page.svelte
|
v
slug = "learn-svelte"
|
v
Page renders with that valueDynamic Routes
Square brackets mark a changing segment. The name inside the brackets becomes a parameter that your page can read.
<script>
import { page } from "$app/stores";
</script>
<h3>Reading post: {$page.params.slug}</h3>Rest parameters such as [...path] match any number of segments. Optional parameters such as [[lang]] match with or without a value.
Layouts
A +layout.svelte file wraps every page in its folder and in all subfolders. Menus, headers, and footers live there. The slot element marks the spot where the active page appears.
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/blog">Blog</a>
</nav>
<main>
<slot />
</main>Svelte 5 projects commonly receive a children prop and render it with the render tag. Both forms achieve the same result.
Layout Nesting Diagram
src/routes/+layout.svelte (site header and footer)
|
+-- blog/+layout.svelte (blog sidebar)
|
+-- [slug]/+page.svelte (single post)
Rendered result: Site frame > Blog sidebar > Post contentNavigation Between Pages
Regular anchor tags work for navigation. SvelteKit intercepts clicks on links that point inside your app, loads the next page in the background, and updates the screen without a full reload. For navigation from code, the goto function handles the job.
<script>
import { goto } from "$app/navigation";
function openPricing() {
goto("/pricing");
}
</script>
<button on:click={openPricing}>See Pricing</button>Special Route Files
File Purpose
------------------- ------------------------------------------
+page.svelte The visible page
+layout.svelte Shared frame around pages
+page.js Load data in the browser or server
+page.server.js Load data on the server only
+server.js Create an API endpoint
+error.svelte Show a friendly error pageRoute Groups
A folder name wrapped in parentheses groups routes without changing the address. A group named (shop) can hold its own layout, and the word shop never appears in the URL.
A Layman Analogy
Think of an apartment building with a directory in the lobby. The floor and door number tell you exactly where to go. SvelteKit reads your folder structure like that directory, and each folder acts as a floor or a door. Square brackets mark a door whose number changes for each visitor.
Common Mistakes To Avoid
- Naming a page file page.svelte without the plus sign
- Mixing helper components into route folders until the folder tree becomes hard to read
- Forgetting the slot or children in a layout, which hides every page
- Using full-page links to external addresses where an internal link would do
Key Points
- Folders in src/routes become URL segments
- Square brackets create dynamic parameters
- Layouts wrap pages and nest through subfolders
- Internal links navigate without a full page reload
