SvelteKit: Definition, Features, Routing, Deployment Guide
SvelteKit is the official Svelte framework for full-stack web apps — file-based routing, SSR/SSG/SPA, adapters for any deploy target.
What is SvelteKit?
SvelteKit is the official application framework for Svelte. Where Svelte is the UI component compiler, SvelteKit is everything else needed to build a full-stack web app: routing, server-side rendering (SSR), static site generation (SSG), API routes, build tooling, and deployment adapters. It's analogous to Next.js for React or Nuxt for Vue.
SvelteKit reached 1.0 in December 2022 and is the recommended way to build any non-trivial Svelte app. It uses Vite for dev/build and ships with first-class TypeScript support.
Why SvelteKit?
- Tiny bundles. Svelte compiles components to vanilla JS — no runtime overhead.
- Fast page loads. SSR + minimal JS = fast LCP, fast INP.
- Flexible rendering. SSR, SSG, SPA, ISR, or hybrid — per route.
- File-based routing. Routes mirror folder structure.
- Adapters for any host. Vercel, Netlify, Cloudflare, Node, static — one config change.
- Form actions. Progressive-enhancement form handling without JS frameworks.
- TypeScript everywhere. First-class, not a retrofit.
- Vite dev server. Fast HMR, modern ESM.
SvelteKit project structure
my-app/
├── src/
│ ├── routes/ # File-based routing
│ │ ├── +page.svelte # / page
│ │ ├── +layout.svelte # Shared layout
│ │ ├── about/
│ │ │ └── +page.svelte # /about
│ │ ├── blog/
│ │ │ ├── +page.svelte # /blog (list)
│ │ │ └── [slug]/
│ │ │ ├── +page.svelte # /blog/:slug
│ │ │ └── +page.server.ts # Server-side load
│ │ └── api/
│ │ └── posts/
│ │ └── +server.ts # API route GET/POST
│ ├── lib/ # Shared modules
│ └── app.html # HTML shell
├── static/ # Static assets
├── svelte.config.js
└── vite.config.tsRouting in SvelteKit
File-based routing maps directly to URL paths. Special files prefixed with +:
| File | Purpose |
|---|---|
+page.svelte | Page component |
+page.server.ts | Server-only data loading |
+page.ts | Universal data loading (server + client) |
+layout.svelte | Layout wrapping nested pages |
+server.ts | API endpoint (GET, POST, etc.) |
+error.svelte | Error boundary |
Data loading example
// src/routes/blog/[slug]/+page.server.ts
import type { PageServerLoad } from './$types';
import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async ({ params }) => {
const post = await db.post.findUnique({ where: { slug: params.slug } });
if (!post) throw error(404, 'Post not found');
return { post };
};<!-- src/routes/blog/[slug]/+page.svelte -->
<script lang="ts">
import type { PageData } from './$types';
export let data: PageData;
</script>
<article>
<h1>{data.post.title}</h1>
<div>{@html data.post.body}</div>
</article>API routes
// src/routes/api/posts/+server.ts
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const GET: RequestHandler = async () => {
const posts = await db.post.findMany();
return json(posts);
};
export const POST: RequestHandler = async ({ request }) => {
const data = await request.json();
const post = await db.post.create({ data });
return json(post, { status: 201 });
};Form actions: progressive enhancement
// +page.server.ts
export const actions: Actions = {
default: async ({ request }) => {
const data = await request.formData();
await db.post.create({ data: { title: data.get('title') as string } });
return { success: true };
}
};<!-- +page.svelte -->
<form method="POST" use:enhance>
<input name="title" />
<button>Save</button>
</form>Works without JS (form posts normally). With JS, use:enhance upgrades to AJAX without page reload.
Adapters: deploy anywhere
| Adapter | Target |
|---|---|
@sveltejs/adapter-auto | Auto-detect from environment |
@sveltejs/adapter-node | Node.js server |
@sveltejs/adapter-static | Pure SSG |
@sveltejs/adapter-vercel | Vercel (serverless + edge) |
@sveltejs/adapter-netlify | Netlify Functions / Edge |
@sveltejs/adapter-cloudflare | Cloudflare Pages / Workers |
@sveltejs/adapter-cloudflare-workers | Worker-only |
SvelteKit vs Next.js vs Nuxt
| Feature | SvelteKit | Next.js (React) | Nuxt (Vue) |
|---|---|---|---|
| Compiler vs runtime | Compiler (no runtime) | Runtime (React) | Runtime (Vue) |
| Bundle size | Smallest | Larger | Medium |
| Learning curve | Easiest (Svelte syntax) | Steeper (hooks) | Medium |
| Ecosystem | Smaller but growing | Largest | Medium |
| Form handling | First-class actions | Server Actions | Composables |
| Deploy targets | Adapters | Vercel-first; Node | Adapters (Nitro) |
SvelteKit best practices
- Use
+page.server.tsfor sensitive data. Server-only code never reaches client. - Use form actions. Progressive enhancement; works without JS.
- Type everything. SvelteKit auto-generates types in
./$types. - Choose the right adapter. Static for pure-content sites; node/serverless for dynamic.
- SSR by default; opt out where needed. Add
export const ssr = falsein+page.tsfor client-only routes. - Streaming with
defer. Stream slow data after fast paint. - Use
$stateand$derived(Svelte 5 runes). New reactivity model.
FAQ: SvelteKit
Is SvelteKit production-ready?
Yes — 1.0 released Dec 2022. Used by NY Times, Apple Music, Spotify Tech Blog, and many others.
SvelteKit or Next.js?
SvelteKit if you want smaller bundles and simpler syntax. Next.js if you need React's massive ecosystem.
Does SvelteKit support TypeScript?
Yes — first-class. Auto-generated types per route in ./$types.
Can I use SvelteKit for a static site?
Yes — adapter-static generates a fully static site. SSG mode, no Node required.
How does SvelteKit handle authentication?
Hooks (src/hooks.server.ts) intercept all requests; set user in event.locals; access in load functions.
Does SvelteKit work with my CSS framework?
Yes — Tailwind, UnoCSS, vanilla CSS, CSS modules all work. Svelte has built-in scoped styles.
What's the difference between Svelte and SvelteKit?
Svelte = UI library / compiler. SvelteKit = full-stack framework using Svelte. Like React vs Next.js.
Load test your SvelteKit app with LoadFocus
LoadFocus runs JMeter and k6 scripts against your SvelteKit deployment from 25+ regions to verify performance under real-world traffic. Sign up free at loadfocus.com/signup.
Related LoadFocus Tools
Put this concept into practice with LoadFocus — the same platform that powers everything you just read about.