Skip to content

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 load free 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

Data loading and mutations

  • 9 SVELTE-5 Never keep user or request data in module-level variables or other shared state on the server: return it from load, pass it down as props or through context, and persist it in a database. Keep load functions 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-6 Load data that needs the database, the filesystem or private credentials in server load functions (+page.server.js), and use universal load functions (+page.js) only for public APIs or for values that can't be serialized. (SvelteKit, Universal vs server: when to use which)
  • 6 SVELTE-7 Avoid request waterfalls: make dependent backend calls in server load functions, use the fetch that load receives, and call await parent() only after the requests that don't depend on it. SvelteKit runs load functions in parallel and inlines the responses of load's fetch into the server-rendered HTML. (SvelteKit, Preventing waterfalls, Making fetch requests, Using parent data)
  • 6 SVELTE-8 Implement mutations as form actions that work without JavaScript, return validation errors with fail(), and progressively enhance the form with use:enhance. Remote functions are an alternative, but they are still experimental. (SvelteKit, Form actions, Progressive enhancement, Remote functions)

Rendering and deployment

Security

  • 9 SVELTE-11 Treat every form action, +server.js handler 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, params and route in the handle hook 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-12 Declare environment variables in src/env.ts, mark one public: true only if its value may be public, and import secrets only from $app/env/private. SvelteKit refuses to import $app/env/private into code that runs in the browser. The older $env/* modules are deprecated. (SvelteKit, Environment variables, Server-only modules)
  • 8 SVELTE-13 Check authentication in the handle hook or in each +page.server.js load function, not only in a +layout.server.js load function. Layout load functions don't rerun on every request, and they run in parallel with page load functions. (SvelteKit, Implications for authentication, Auth: integration points)
  • 7 SVELTE-14 Put code that uses secrets, the database or other server-only APIs in server-only modules: files with a server segment in their name, such as db.server.ts, or files in a server directory. 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-15 Set each page's <title> in <svelte:head>, and the lang attribute in src/app.html. SvelteKit announces the title to screen readers after each client-side navigation, which is how SvelteKit apps apply WEB-22. (SvelteKit, Route announcements, The lang attribute)
  • 6 SVELTE-16 Fix 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. Checks SVELTE-3 and SVELTE-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/server or a server-only module, directly or indirectly. Checks SVELTE-14, and partly SVELTE-12.
  • 6 eslint-plugin-svelte: svelte/prefer-writable-derived (recommended) flags $state that an $effect only keeps in sync, svelte/valid-compile reports compiler warnings, including accessibility warnings, as lint problems, and svelte/no-unused-svelte-ignore flags ignore comments that no longer suppress anything. Checks SVELTE-16, and partly SVELTE-2.
  • 6 Svelte compiler runes option: forces runes mode, so legacy syntax such as $: and export let fails to compile. Set it with a function that returns true only for your own files, because true also forces components in node_modules into runes mode. Partly checks SVELTE-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.