Svelte
Compiler-based UI framework that ships zero runtime - write reactive components in plain JS/HTML/CSS, deploy full-stack apps with SvelteKit.
skill: Svelte category: Web Development tags: [Svelte, SvelteKit, Compiler, JavaScript, Frontend] readingTime: 6
Svelte is ideal when bundle size and runtime performance are the primary constraints - it ships no framework runtime. SvelteKit is the full-stack layer. For larger teams with React expertise, React/Next.js is safer. For Vue teams, Vue+Nuxt is more familiar.
Workflow
- Create a SvelteKit projectSvelteKit is Svelte's official full-stack framework - handles routing, SSR, form actions, and adapters for Vercel/Cloudflare/Node.
- Write Svelte componentsSvelte compiles template syntax to minimal vanilla JS - no virtual DOM diffing at runtime, smallest possible output.
- Use runes for reactivity (Svelte 5)Svelte 5 runes ($state, $derived, $effect) make reactivity explicit and composable - replacing the magic variable assignment of Svelte 4.
- Handle data loading with +page.server.tsSvelteKit runs load functions on the server, keeping DB credentials out of the browser and enabling SSR.
- Submit forms with form actionsForm actions handle POST submissions server-side with progressive enhancement - works without JS and upgrades to fetch when available.
Output Format
SvelteKit project with file-based routing, server load functions, and form actions.
my-app/
├── src/
│ ├── routes/
│ │ ├── +layout.svelte # Root layout
│ │ ├── +page.svelte # Home page
│ │ └── posts/
│ │ ├── +page.server.ts # Server data load
│ │ └── +page.svelte
│ └── lib/
│ └── db.ts
└── svelte.config.jsExamples
Input
Output
Edge Cases & Gotchas
- Runes vs legacy reactivity - Svelte 5 runes ($state) and Svelte 4 reactive assignments ($: ) don't mix within the same component. Migrate one component at a time.
- SSR environment mismatch - browser APIs (window, localStorage) are undefined on the server. Guard with: if (browser) { ... } using SvelteKit's $app/environment.
- Store memory leaks - Subscribing to a store in component JS (not template) requires unsubscribing in onDestroy, or use the $ shorthand which auto-manages lifecycle.
- Form action error handling - When a form action throws, SvelteKit returns the error as `form.error`. Use the `fail()` helper to return structured errors without throwing.
When to Break These Rules
- Use React/Next.js when your team is large, hiring market favors React, or you need a vast ecosystem of pre-built components.
- Use Astro instead of SvelteKit for marketing/documentation sites that are mostly static - Astro natively supports Svelte components too.
Reference Files
https://svelte.dev/docshttps://kit.svelte.dev/docs