Svelte and SvelteKit¶
Scope: web apps built with Svelte 5 in runes mode and SvelteKit, following the current (SvelteKit 3) documentation. Framework-independent rules for accessibility, performance and security, such as WCAG, Core Web Vitals, sanitizing HTML before {@html} and Content Security Policy, are in Web front end. Server load functions, form actions, +server.js handlers and remote functions are backend code, so the Node.js and Security guidelines also apply to them.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 9 Svelte documentation: the baseline for components: runes, template syntax, styling, special elements, runtime APIs such as context and lifecycle, testing, TypeScript, and the compiler's errors and warnings.
- 9 SvelteKit documentation: the baseline for apps: project structure, routing, loading data, form actions, page options, state management, hooks, adapters, environment variables, server-only modules, and best practices for auth, performance and accessibility.
- 9 Svelte, Best practices: the official advice on
$state,$derived,$effect, props, events, snippets, keyed each blocks, styling, context, and the legacy features to avoid. - 8 SvelteKit, State management: avoiding shared state on the server, keeping
loadfree of side effects, and keeping state in context, the URL or snapshots. - 8 SvelteKit, Performance: what SvelteKit optimizes by default, and how to diagnose issues, optimize assets, reduce code size, prevent waterfalls and choose hosting.
- 7 SvelteKit, Accessibility: the built-in route announcements and focus management, and what each app still has to provide.
- 7 Migrating to SvelteKit v3: the breaking changes in SvelteKit 3, such as configuration in
vite.config.js, the new environment variable modules, the wider server-only module rules and the CSRF settings, with an automatic migration command.
Key rules¶
Components and reactivity¶
- 7
SVELTE-1Write new components in runes mode, and don't use legacy features that have modern replacements, such as$:statements,export let,on:directives, slots, and stores for shared state. Runes mode will be the compiler's default in Svelte 6. (Svelte, Best practices: avoid legacy features, svelte/compiler, CompileOptions) - 7
SVELTE-2Compute values from state and props with$derived, not by assigning state in an$effect, and keep$effectas an escape hatch for things such as analytics and direct DOM manipulation. Handle user interaction in event handlers. A value computed once from props goes stale, because SvelteKit reuses page and layout components when the user navigates. (Svelte, When not to use $effect, Best practices: $props, SvelteKit, Component and page state is preserved) - 7
SVELTE-3Type-check.sveltefiles in CI withsv check(svelte-check). The editor extension shows the same errors while you work. (Svelte, TypeScript, sv check) - 6
SVELTE-4Share reactive state between components with$stateobjects or classes with$statefields, defined in.svelte.jsor.svelte.tsmodules, and keep stores for complex asynchronous streams or for when you need manual control over updates. In server-rendered apps, provide that state through context rather than from a shared module. (Svelte, When to use stores, Best practices: avoid legacy features, Best practices: context)
Data loading and mutations¶
- 9
SVELTE-5Never keep user or request data in module-level variables or other shared state on the server: return it fromload, pass it down as props or through context, and persist it in a database. Keeploadfunctions free of side effects for the same reason. A server is long-lived and shared by all users, so such state leaks one user's data to the next. (SvelteKit, Avoid shared state on the server, No side-effects in load, Svelte, Context: replacing global state) - 7
SVELTE-6Load data that needs the database, the filesystem or private credentials in serverloadfunctions (+page.server.js), and use universalloadfunctions (+page.js) only for public APIs or for values that can't be serialized. (SvelteKit, Universal vs server: when to use which) - 6
SVELTE-7Avoid request waterfalls: make dependent backend calls in serverloadfunctions, use thefetchthatloadreceives, and callawait parent()only after the requests that don't depend on it. SvelteKit runsloadfunctions in parallel and inlines the responses ofload'sfetchinto the server-rendered HTML. (SvelteKit, Preventing waterfalls, Making fetch requests, Using parent data) - 6
SVELTE-8Implement mutations as form actions that work without JavaScript, return validation errors withfail(), and progressively enhance the form withuse:enhance. Remote functions are an alternative, but they are still experimental. (SvelteKit, Form actions, Progressive enhancement, Remote functions)
Rendering and deployment¶
- 7
SVELTE-9Choose rendering per route with page options: prerender pages that every user sees the same, server-render dynamic pages, and turn offssronly for pages that can't run on the server. Prerendered content is shown to all users, and SPA mode adds network round trips before the first render. (SvelteKit, Page options, When not to prerender, Preventing waterfalls) - 5
SVELTE-10Replaceadapter-autowith the adapter for your platform once you've chosen where to deploy, and host the app in the same data center as its backend. (SvelteKit, Zero-config deployments, Performance: hosting)
Security¶
- 9
SVELTE-11Treat every form action,+server.jshandler and remote function as a public HTTP endpoint: make sure an authorization check covers it, and validate its input on the server. Give remote functions a Standard Schema for their arguments. For remote function requests,url,paramsandroutein thehandlehook describe the calling page and can be manipulated, so never base authorization on them. (SvelteKit, Remote functions: query arguments, Hooks: handle, Form actions: validation errors) - 9
SVELTE-12Declare environment variables insrc/env.ts, mark onepublic: trueonly if its value may be public, and import secrets only from$app/env/private. SvelteKit refuses to import$app/env/privateinto code that runs in the browser. The older$env/*modules are deprecated. (SvelteKit, Environment variables, Server-only modules) - 8
SVELTE-13Check authentication in thehandlehook or in each+page.server.jsloadfunction, not only in a+layout.server.jsloadfunction. Layoutloadfunctions don't rerun on every request, and they run in parallel with pageloadfunctions. (SvelteKit, Implications for authentication, Auth: integration points) - 7
SVELTE-14Put code that uses secrets, the database or other server-only APIs in server-only modules: files with aserversegment in their name, such asdb.server.ts, or files in aserverdirectory. SvelteKit then raises an error when code that runs in the browser imports them, even indirectly. (SvelteKit, Server-only modules: your modules)
Accessibility¶
- 6
SVELTE-15Set each page's<title>in<svelte:head>, and thelangattribute insrc/app.html. SvelteKit announces the title to screen readers after each client-side navigation, which is how SvelteKit apps applyWEB-22. (SvelteKit, Route announcements, The lang attribute) - 6
SVELTE-16Fix the compiler's accessibility warnings, and silence one with<!-- svelte-ignore -->only for a false positive, with a note saying why. (Svelte, Compiler warnings)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 sv check: runs svelte-check, which reports TypeScript errors and compiler warnings, including accessibility warnings. In CI, run it with
--fail-on-warnings, or turn chosen warnings into errors with--compiler-warnings. ChecksSVELTE-3andSVELTE-16. - 7 Server-only module check: built into SvelteKit, which raises an error when code that runs in the browser imports
$app/env/private,$app/serveror a server-only module, directly or indirectly. ChecksSVELTE-14, and partlySVELTE-12. - 6 eslint-plugin-svelte:
svelte/prefer-writable-derived(recommended) flags$statethat an$effectonly keeps in sync,svelte/valid-compilereports compiler warnings, including accessibility warnings, as lint problems, andsvelte/no-unused-svelte-ignoreflags ignore comments that no longer suppress anything. ChecksSVELTE-16, and partlySVELTE-2. - 6 Svelte compiler
runesoption: forces runes mode, so legacy syntax such as$:andexport letfails to compile. Set it with a function that returnstrueonly for your own files, becausetruealso forces components innode_modulesinto runes mode. Partly checksSVELTE-1.
Tutorials and further reading¶
- 7 Svelte tutorial: the official interactive tutorial in four parts: basic Svelte, advanced Svelte, basic SvelteKit and advanced SvelteKit.
- 6 Testing (Svelte): unit and component tests with Vitest, component tests with Storybook, and end-to-end tests with Playwright.