CLAUDE.md rules for Svelte 5 and SvelteKit 2 with runes, callback props, server load functions, unthrown error and redirect calls, and validated form actions.
.claude/rules/claude-svelte-developer.md. Downloaded files need to be placed in that repository path.---
paths:
- "**/*"
---
# Svelte and SvelteKit Conventions
Applies to: `.svelte` components and SvelteKit routes (`+page.svelte`, `+page.server.ts`, `+layout.ts`, form actions) in this repository.
Version baseline: checked against Svelte 5 (runes) and SvelteKit 2. Read `package.json`. If the project is still on Svelte 4, keep `export let`, `$:` and `on:click` in existing components and don't mix syntaxes inside one component.
## Components (Svelte 5 runes)
- Declare props with `$props()` (destructure with defaults), local state with `$state()`, and derived values with `$derived()`. Don't use `export let` or `$:` in new code.
- Use event attributes (`onclick={...}`), not `on:click`. Instead of the deprecated `createEventDispatcher`, accept callback props (`onupdate`, `ondelete`).
- Load data in `load` functions, not `onMount`. That way it is available during SSR and shows no loading flash. Use `$effect` only to sync with external systems, never to derive state.
- Type props with a `Props` type in `<script lang="ts">`.
```svelte
<script lang="ts">
import type { User } from '$lib/types';
type Props = {
user: User;
editable?: boolean;
onupdate?: (user: User) => void;
};
let { user, editable = false, onupdate }: Props = $props();
let fullName = $derived(`${user.firstName} ${user.lastName}`);
</script>
<article>
<h2>{fullName}</h2>
{#if editable && onupdate}
<button type="button" onclick={() => onupdate(user)}>Edit</button>
{/if}
</article>
```
## Loading data (SvelteKit 2)
- Server-only data (database, secrets) goes in `+page.server.ts`. Universal fetches go in `+page.ts`.
- Call `error(...)` and `redirect(...)` directly. Since SvelteKit 2 you don't `throw` them, and you shouldn't call them inside a `try` block.
- Use generated types from `./$types` (`PageServerLoad`, `Actions`).
```ts
// src/routes/posts/[id]/+page.server.ts
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { db } from '$lib/server/db';
export const load: PageServerLoad = async ({ params }) => {
const post = await db.post.findUnique({ where: { id: params.id } });
if (!post) error(404, 'Post not found');
return { post };
};
```
## Form actions
- Validate on the server. Return `fail(status, data)` with the field values and errors so the form can re-render, then `redirect(303, ...)` after success.
- Enhance forms with `use:enhance` from `$app/forms` so they still work without JavaScript.
```ts
// src/routes/posts/new/+page.server.ts
import { error, fail, redirect } from '@sveltejs/kit';
import type { Actions } from './$types';
import { db } from '$lib/server/db';
export const actions: Actions = {
create: async ({ request, locals }) => {
if (!locals.user) error(401, 'Sign in required');
const data = await request.formData();
const title = String(data.get('title') ?? '').trim();
if (!title) return fail(400, { title, error: 'Title is required' });
const post = await db.post.create({ data: { title, authorId: locals.user.id } });
redirect(303, `/posts/${post.id}`);
},
};
```
## State and stores
- Read page data and URL state from `$app/state` (`page`). `$app/stores` is deprecated.
- For shared client state, use a `.svelte.ts` module exporting `$state` objects, or context. Keep server data in `load` results, not global stores, to avoid leaking data between users during SSR.
## Verify before finishing
Run when available: `npx sv check` (or `npm run check`, which runs svelte-check), `npx eslint .` and the test command (Vitest/Playwright). For a Svelte 4 component you are migrating, `npx sv migrate svelte-5` exists, but review its diff.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Stack-neutral CLAUDE.md conventions for Claude Code covering how to read before editing, keep diffs scoped, debug from a reproduction, test and report verification.
Cursor rules for React with TypeScript: typed function components, React 19 ref-as-prop, hooks discipline, state ownership, error boundaries and measured performance work.
Claude
frontend
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Claude Code edits a SvelteKit 2 app on Svelte 5 with this file loaded as a project rule scoped to src/**/*.{svelte,ts}.
UserCard.svelte uses export let, createEventDispatcher, on:click and fetches the user in onMount. posts/[id]/+page.server.ts uses `throw error(404)`. Modernize both and add a create-post form action requiring a signed-in user. svelte ^5, @sveltejs/kit ^2.
Illustrative; not a recorded model run. UserCard rewritten with $props (typed, defaults), $derived for fullName, onclick and an onupdate callback prop; the user is loaded in a load function instead of onMount; error(404, ...) called without throw; a create action that checks locals.user, validates title, returns fail(400, { title, error }) and calls redirect(303, ...); use:enhance on the form; npx sv check output reported.Claude Code loads persistent project instructions from CLAUDE.md files and from Markdown rules in .claude/rules/. Rules can be scoped to file paths with a paths frontmatter field, and CLAUDE.md can import other files with @path syntax. The rules in this directory are written as plain instruction prompts: in Claude Code, save them as CLAUDE.md or .claude/rules/ content; in Claude.ai or the Claude API, use the same text as project instructions or a system prompt instead.
.claude/rules/claude-svelte-developer.mdStarting point, not a tested configuration for your repository. Loaded when Claude Code works with files matching paths. Review the instructions, adapt the file paths and run your project's existing checks.
---
paths:
- "**/*"
---
# Svelte and SvelteKit Conventions
Applies to: `.svelte` components and SvelteKit routes (`+page.svelte`, `+page.server.ts`, `+layout.ts`, form actions) in this repository.
Version baseline: checked against Svelte 5 (runes) and SvelteKit 2. Read `package.json`. If the project is still on Svelte 4, keep `export let`, `$:` and `on:click` in existing components and don't mix syntaxes inside one component.
## Components (Svelte 5 runes)
- Declare props with `$props()` (destructure with defaults), local state with `$state()`, and derived values with `$derived()`. Don't use `export let` or `$:` in new code.
- Use event attributes (`onclick={...}`), not `on:click`. Instead of the deprecated `createEventDispatcher`, accept callback props (`onupdate`, `ondelete`).
- Load data in `load` functions, not `onMount`. That way it is available during SSR and shows no loading flash. Use `$effect` only to sync with external systems, never to derive state.
- Type props with a `Props` type in `<script lang="ts">`.
```svelte
<script lang="ts">
import type { User } from '$lib/types';
type Props = {
user: User;
editable?: boolean;
onupdate?: (user: User) => void;
};
let { user, editable = false, onupdate }: Props = $props();
let fullName = $derived(`${user.firstName} ${user.lastName}`);
</script>
<article>
<h2>{fullName}</h2>
{#if editable && onupdate}
<button type="button" onclick={() => onupdate(user)}>Edit</button>
{/if}
</article>
```
## Loading data (SvelteKit 2)
- Server-only data (database, secrets) goes in `+page.server.ts`. Universal fetches go in `+page.ts`.
- Call `error(...)` and `redirect(...)` directly. Since SvelteKit 2 you don't `throw` them, and you shouldn't call them inside a `try` block.
- Use generated types from `./$types` (`PageServerLoad`, `Actions`).
```ts
// src/routes/posts/[id]/+page.server.ts
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { db } from '$lib/server/db';
export const load: PageServerLoad = async ({ params }) => {
const post = await db.post.findUnique({ where: { id: params.id } });
if (!post) error(404, 'Post not found');
return { post };
};
```
## Form actions
- Validate on the server. Return `fail(status, data)` with the field values and errors so the form can re-render, then `redirect(303, ...)` after success.
- Enhance forms with `use:enhance` from `$app/forms` so they still work without JavaScript.
```ts
// src/routes/posts/new/+page.server.ts
import { error, fail, redirect } from '@sveltejs/kit';
import type { Actions } from './$types';
import { db } from '$lib/server/db';
export const actions: Actions = {
create: async ({ request, locals }) => {
if (!locals.user) error(401, 'Sign in required');
const data = await request.formData();
const title = String(data.get('title') ?? '').trim();
if (!title) return fail(400, { title, error: 'Title is required' });
const post = await db.post.create({ data: { title, authorId: locals.user.id } });
redirect(303, `/posts/${post.id}`);
},
};
```
## State and stores
- Read page data and URL state from `$app/state` (`page`). `$app/stores` is deprecated.
- For shared client state, use a `.svelte.ts` module exporting `$state` objects, or context. Keep server data in `load` results, not global stores, to avoid leaking data between users during SSR.
## Verify before finishing
Run when available: `npx sv check` (or `npm run check`, which runs svelte-check), `npx eslint .` and the test command (Vitest/Playwright). For a Svelte 4 component you are migrating, `npx sv migrate svelte-5` exists, but review its diff.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Imports with @path can nest up to four hops deep, according to the docs. Claude Code can read AGENTS.md as project instructions without extra configuration, but by default only when there is no CLAUDE.md in the working directory or above it. The docs list this behavior for version 2.1.277 and later and describe a setting (claude-md-and-agents-md) that loads both files. If you maintain AGENTS.md for other tools, you can also import it from CLAUDE.md with @AGENTS.md.
Documentation checked 2026-11-05: Claude Code docs: Manage Claude's memory
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Stack-neutral CLAUDE.md conventions for Claude Code covering how to read before editing, keep diffs scoped, debug from a reproduction, test and report verification.
Cursor rules for React with TypeScript: typed function components, React 19 ref-as-prop, hooks discipline, state ownership, error boundaries and measured performance work.