SvelteKit

The official Svelte application framework with file-based routing, load functions, form actions and flexible rendering.

Intermediate · about 8 hours · difficulty 3/5

Learn first: CSR, SSR, SSG, ISR and Streaming, Svelte

SvelteKit is the full-stack framework for Svelte. It combines Svelte's small, fast components with routing, server rendering, data loading and deployment adapters.

Core concepts

  • Routing from src/routes: +page.svelte for a page, +layout.svelte for shared layouts, [param] folders for dynamic segments and +error.svelte for errors.
  • Load functions: +page.ts (runs on server and client) or +page.server.ts (server only, safe for secrets and databases) return data that the page receives as a prop.
  • Form actions in +page.server.ts handle <form method="POST"> submissions, with use:enhance for progressive enhancement.
  • API routes with +server.ts for JSON endpoints.
  • Hooks (hooks.server.ts) for auth, logging and request handling.
  • Rendering options per route: SSR, prerendering (export const prerender = true) or CSR only.
  • Adapters deploy to Node, Vercel, Netlify, Cloudflare or static hosts.

Pitfalls

Putting secrets in universal +page.ts loads (they also run in the browser), forgetting validation in form actions, and mixing Svelte 4 and Svelte 5 syntax from older tutorials.

Good enough to move on

You can build a SvelteKit app with layouts, server load functions, form actions with validation and progressive enhancement, and deploy it with an adapter.

Key concepts

  • Routes, pages and layouts
  • Universal vs server load functions
  • Form actions and use:enhance
  • API routes with +server.ts
  • Hooks
  • Prerendering and rendering options
  • Deployment adapters

Other options for this step

  • Next.js: The leading React framework: App Router, layouts, Server Components, data fetching and caching, Server Actions and middleware.
  • React Router Framework Mode (Remix): Web-standards-first full-stack React with loaders, actions and nested routes; the evolution of Remix.
  • Nuxt: The full-stack framework for Vue: file-based routing, SSR and SSG, auto-imports, data fetching and server routes.
  • Astro: A content-first framework that ships zero JavaScript by default and hydrates interactive islands built with any UI library.

Mini project

Build a link-sharing app in SvelteKit with server load functions reading from a database, form actions to add and upvote links with validation, a hooks-based session check and deployment with an adapter.

Check yourself

Why should a database query live in +page.server.ts rather than +page.ts?

Free resources