Skip to content

project rulesFrontendsvelte

SvelteKit Rules

Project rules for SvelteKit: load functions, form actions, Svelte 5 runes, server-only modules and clear client and server boundaries.

npx workspacetemplates add sveltekit-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file19 lines · 1.4 KBFile
# Project rules: SvelteKit

## Routing and data
- Routes live in `src/routes`. Fetch page data in `+page.server.ts` or `+page.ts` `load` functions, not in `onMount`. Use the provided `fetch` so cookies and relative URLs work during server rendering.
- Return plain serializable data from `load`. Use `depends` and `invalidate` when data must refresh.
- Put database access, secrets and private environment variables in `+server.ts`, `+page.server.ts` and `$lib/server/`. Import secrets only from `$env/static/private` or `$env/dynamic/private`, never from client code.

## Mutations
- Use form actions in `+page.server.ts` with real `<form method="POST">` elements and `use:enhance` for progressive enhancement. Validate with Zod, and return `fail(400, { errors })` rather than throwing for user mistakes.
- Use `error()` for 404 and 403 and `redirect()` after a successful mutation.

## Svelte 5
- Use runes: `$state`, `$derived`, `$effect` (only for side effects), `$props`. Use `{@render}` snippets instead of slots in new code.
- Keep components small, and put reusable code in `$lib` with the alias, not long relative paths.
- Handle auth in `hooks.server.ts` and set `event.locals`; check it in each protected load.

## Quality
- Use TypeScript with generated `$types`. Test logic with Vitest and flows with Playwright.
- Run `svelte-check`, the linter and the tests before finishing.

What it does

This rules file keeps an agent inside the SvelteKit model: data comes from load functions, mutations go through form actions, and server code stays on the server.

It also sets the Svelte 5 style, using runes for state, so new components are consistent and free of legacy store patterns where they are not needed.

Example

you › Add a newsletter signup form to the footer

agent › The agent adds a form action with Zod validation and fail(400) on errors, enhances the form with use:enhance, keeps the email service key in $lib/server, and adds a Playwright check.

Questions

Why prefer load over onMount fetching?
Load runs on the server for the first request, so the page arrives with data and works for crawlers without waiting for client code.
Are Svelte stores banned?
No. They remain fine for shared reactive state, but local component state should use runes.
  • .NET Blazor Rules

    Project rulescsharp

    Free
  • Angular Rules

    Project rulesangular

    Free
  • Astro Rules

    Project rulesastro

    Free
  • Electron Rules

    Project ruleselectron

    Free