SvelteKit
SvelteKit: build fast web apps with Svelte
Why SvelteKit is worth using
SvelteKit is the official app framework for Svelte. Svelte is a compiler: it turns your components into small, plain JavaScript at build time instead of shipping a big framework runtime to the browser. SvelteKit adds routing, data loading, server code, and builds on top of that.
- Less code to write. Svelte components are mostly plain HTML, CSS, and JavaScript. Reactivity is built into the language with runes like
$state, so you don’t need hooks or extra state libraries. - Small and fast in the browser. The compiler outputs only the code your page needs, so bundles stay small and pages load quickly.
- Routing from your folders. Add a folder with a
+page.svelteinsidesrc/routesand you have a new page. - Server rendering by default. The first visit is rendered on the server for fast loads and good SEO. After that the app takes over in the browser for quick navigation between pages.
- Frontend and backend in one project.
loadfunctions, form actions, and+server.jsendpoints let you fetch data and handle forms next to your pages. - Deploy almost anywhere. Adapters build your app for Node.js, Vercel, Netlify, Cloudflare, or as a plain static site.
- Built on Vite. You get a quick dev server with instant hot reloading.
Installing SvelteKit
You need a current LTS release of Node.js. Check your version with:
node -v
There’s nothing to install globally. The sv command line tool creates the project for you:
npx sv create my-app
It asks you a few questions:
- Template:
minimalfor a blank app,demofor a small example game, orlibraryfor building a Svelte package - Type checking: TypeScript, JavaScript with JSDoc, or none
- Add-ons: optional extras such as Tailwind CSS, ESLint, Prettier, Vitest, Playwright, and more
- Package manager: npm, pnpm, yarn, bun, or deno
Setting up a new app
npx sv create my-app– create the projectcd my-appnpm install– install dependencies (skip this if you already did it during setup)npm run dev– start the dev server- Open
http://localhost:5173 - Edit
src/routes/+page.svelteand save. The browser updates right away.
Project layout at a glance
src/routes/– your routes. Each folder is a URL segment and each+page.svelteis a pagesrc/lib/– shared components and utilities, imported with the$libaliassrc/lib/server/– server-only code (secrets, database access) that can never be sent to the browsersrc/app.html– the HTML template every page is rendered intostatic/– files copied as-is (images, favicons,robots.txt)svelte.config.js– SvelteKit config, including which adapter to usevite.config.js– Vite config
Route file names
+page.svelte– the page component+page.js– aloadfunction that runs on the server and in the browser+page.server.js– aloadfunction and form actions that only run on the server+layout.svelte– a layout that wraps the pages in its folder and all its subfolders+server.js– an API endpoint (GET,POST, etc.)
Adding a page
Create src/routes/about/+page.svelte:
<script>
let count = $state(0);
</script>
<h1>About me</h1>
<button onclick={() => count++}>
Clicked {count} times
</button>
Visit http://localhost:5173/about and it’s there.
Run the SvelteKit server for dev
npm run dev (runs vite dev)
Useful flags:
npm run dev -- --open– open the site in your browser automaticallynpm run dev -- --port 4000– use a different portnpm run dev -- --host– make the server reachable from other devices on your network
Build for deployment
npm run build– production buildnpm run preview– preview the production build locally before you deploy
How the build is packaged depends on your adapter, which is set in svelte.config.js:
@sveltejs/adapter-auto– the default. It detects supported hosts like Vercel, Netlify, and Cloudflare@sveltejs/adapter-node– a standalone Node.js server you can run anywhere@sveltejs/adapter-static– a plain static site
Exporting a static site
npm i -D @sveltejs/adapter-static- Change the adapter in
svelte.config.js:
import adapter from '@sveltejs/adapter-static';
export default {
kit: {
adapter: adapter()
}
};
- Prerender every page by adding this to
src/routes/+layout.js:
export const prerender = true;
npm run build
The finished site is written to the build/ folder. Upload that folder to any static host.
note
- Features that need a server at request time, such as form actions and
+page.server.jscode that reads cookies, won’t work in a static build - On GitHub Pages, if your repo isn’t named
your-username.github.io, setkit.paths.baseinsvelte.config.jsto your repo name - Add more add-ons to an existing project later with
npx sv add(for examplenpx sv add tailwindcss)