Routing with SvelteKit
React has no built-in router. Most React apps use react-router (declarative <Route> components) or a framework like Next.js (file-based routing in pages/ or app/). SvelteKit is Svelte’s full-stack framework, and routing is baked in — you never install a router package.
The shift is simple: your file system is your route map. Every file you create under src/routes/ becomes a URL. No JSX route config, no createBrowserRouter, no <Routes> wrapper.
Run this in a SvelteKit project (
npx sv create).
The src/routes/ directory
Section titled “The src/routes/ directory”SvelteKit reserves the src/routes/ directory for your route tree. The structure of that directory is the structure of your app’s URLs.
| File | URL |
|---|---|
src/routes/+page.svelte | / |
src/routes/about/+page.svelte | /about |
src/routes/blog/[slug]/+page.svelte | /blog/:slug |
src/routes/dashboard/+layout.svelte | wraps all /dashboard/* pages |
Special filenames at a glance
Section titled “Special filenames at a glance”SvelteKit uses a convention of + prefixed filenames so that SvelteKit-owned files are never confused with your own helper modules in the same folder.
| Filename | Purpose |
|---|---|
+page.svelte | The page UI rendered at this route |
+page.js | load() function — runs on server and client |
+page.server.js | load() function — runs on server only (DB, cookies, secrets) |
+layout.svelte | Shared shell wrapping all child routes in this folder |
+layout.js / +layout.server.js | load() for the layout |
+error.svelte | Error boundary for this route segment |
How this maps from React
Section titled “How this maps from React”If you’re coming from react-router, the mental model shift is:
- react-router: you write a JS/JSX config that maps path strings to component imports.
- Next.js Pages Router:
pages/folder, files map to routes. - Next.js App Router:
app/folder,page.tsxfiles inside folders map to routes. - SvelteKit:
src/routes/folder,+page.sveltefiles inside folders map to routes.
SvelteKit is closest to Next.js App Router, but uses +page.svelte instead of page.tsx and collocates data-loading in +page.js / +page.server.js rather than making the component itself async.
What this module covers
Section titled “What this module covers”| Lesson | Concept |
|---|---|
| This page | Mental model: file-based routing, the src/routes/ directory |
| File-based routing | Nested routes, [param] dynamic segments, file-tree walkthrough |
| Load functions | +page.js / +page.server.js for data fetching — vs react-router loaders and getServerSideProps |