Skip to content

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).

SvelteKit reserves the src/routes/ directory for your route tree. The structure of that directory is the structure of your app’s URLs.

FileURL
src/routes/+page.svelte/
src/routes/about/+page.svelte/about
src/routes/blog/[slug]/+page.svelte/blog/:slug
src/routes/dashboard/+layout.sveltewraps all /dashboard/* pages

SvelteKit uses a convention of + prefixed filenames so that SvelteKit-owned files are never confused with your own helper modules in the same folder.

FilenamePurpose
+page.svelteThe page UI rendered at this route
+page.jsload() function — runs on server and client
+page.server.jsload() function — runs on server only (DB, cookies, secrets)
+layout.svelteShared shell wrapping all child routes in this folder
+layout.js / +layout.server.jsload() for the layout
+error.svelteError boundary for this route segment

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.tsx files inside folders map to routes.
  • SvelteKit: src/routes/ folder, +page.svelte files 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.

LessonConcept
This pageMental model: file-based routing, the src/routes/ directory
File-based routingNested routes, [param] dynamic segments, file-tree walkthrough
Load functions+page.js / +page.server.js for data fetching — vs react-router loaders and getServerSideProps
In which directory do SvelteKit routes live?
What does a file named +page.svelte represent in SvelteKit?
How does SvelteKit routing differ from react-router?