Skip to content

How to understand a SvelteKit codebase

SvelteKit uses folders under `src/routes/` as URLs, and special `+` files decide what each folder does: `+page.svelte` renders, `+page.ts` or `+page.server.ts` loads data, `+server.ts` serves an endpoint. `hooks.server.ts` runs on every request.

How to tell it's SvelteKit

  • @sveltejs/kit in package.json and svelte.config.js with an adapter
  • Files named +page.svelte, +layout.svelte, +page.server.ts and +server.ts
  • Imports from $lib, $app/navigation or $env/static/private

Where things usually live

src/routes/**/+page.svelte
The page component.
src/routes/**/+page.ts
Universal load: runs on the server first, then in the browser on navigation.
src/routes/**/+page.server.ts
Server-only load and form actions.
src/routes/**/+server.ts
API endpoints exporting GET, POST and so on.
src/hooks.server.ts
handle runs for every request: auth, sessions, headers.
src/lib/
Shared code ($lib); src/lib/server/ can only be imported on the server.
svelte.config.js
Adapter (Node, Vercel, static and others), aliases, preprocessors.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. src/hooks.server.ts, because it affects every request
  2. The route folder for the URL: +page.server.ts first, then +page.svelte
  3. src/lib/server/, for database and secrets

How a request moves through the code

  1. 1handle in hooks.server.ts runs, often populating event.locals with the user.
  2. 2Layout and page load functions run, from the outermost layout inward.
  3. 3+page.svelte receives the merged data as its data prop.
  4. 4Form submissions hit actions in +page.server.ts; use:enhance makes them work without a full reload.
  5. 5Endpoints in +server.ts serve fetch calls from the page or from outside.

Questions to ask about the repository

  • What does hooks.server.ts put on locals, and which routes rely on it?
  • Which load functions run only on the server and which also in the browser?
  • Where are form actions defined, and how are they validated?
  • Which adapter does this app deploy with?

What trips people up

  • +page.ts code also runs in the browser, so it must not touch secrets; that belongs in +page.server.ts.
  • Layout load data is inherited by child pages, so a value can appear in a page without being loaded there.
  • Older SvelteKit code uses different file names (__layout.svelte); check the version if names don't match.