SvelteKit architecture rules: routes as +page/+layout/+server files, server-only code in $lib/server, load functions instead of client API wrappers, and no per-user state in modules.
.cursor/rules/sveltekit-architecture-rules.mdc. Downloaded files need to be placed in that repository path.---
description: "SvelteKit architecture rules: routes as +page/+layout/+server files, server-only code in $lib/server, load functions instead of client API wrappers, and no per-user state in modules."
globs: "**/*"
alwaysApply: false
---
Apply these rules when structuring a SvelteKit application.
## Principles
- Keep each component and module focused on one responsibility; extract reusable UI into `src/lib/components`.
- Keep data loading and mutations on the server side of a route (`+page.server.ts`, `+layout.server.ts`, `+server.ts`) and pass plain data to components.
## Directory structure
```
src/
├── lib/
│ ├── components/ # reusable components (PascalCase.svelte)
│ ├── server/ # server-only code: db, auth, secrets (cannot be imported by client code)
│ └── state/ # shared client state (*.svelte.ts)
├── routes/
│ ├── +layout.svelte # layouts live in routes, next to the pages they wrap
│ ├── +page.svelte
│ ├── users/[id]/
│ │ ├── +page.svelte
│ │ └── +page.server.ts
│ └── api/users/+server.ts # JSON endpoints
├── hooks.server.ts # auth/session → event.locals
└── app.d.ts # App.Locals, App.PageData, App.Error types
```
- Routes are directories: a dynamic page is `src/routes/users/[id]/+page.svelte`, not `users/[id].svelte`.
- Layouts are `+layout.svelte` files in the route tree; use route groups such as `(app)` and `(marketing)` for different layouts without changing URLs.
- Put database clients, secrets and auth helpers in `$lib/server` or `*.server.ts` files; SvelteKit refuses to bundle them into client code.
## Naming
- Component files use PascalCase (`UserCard.svelte`); other modules follow the project's convention (kebab-case or camelCase), consistently.
- Variables and functions use camelCase; types and interfaces PascalCase.
## Types
- Use the generated `./$types` (`PageServerLoad`, `PageProps`, `Actions`, `RequestHandler`) for load functions, actions and endpoints, and declare `App.Locals` in `app.d.ts`.
## Data and API access
- Load page data in `load` functions and use the `fetch` passed to `load` (it forwards cookies and works during server rendering). Do not import `node-fetch`; modern Node and browsers provide `fetch`.
- Use `+server.ts` endpoints for JSON consumed by other clients; pages in the same app can read data from `load` directly instead of calling their own API.
- Use form actions for mutations from pages so forms work without JavaScript and progressively enhance with `use:enhance`.
## State
- Component-local state stays in the component (`$state` in Svelte 5).
- Shared client state lives in `.svelte.ts` modules or stores. On the server, modules are shared across all requests, so never keep per-user data (session, cart) in module-level state; use `event.locals`, `load` data or component context.
## Common pitfalls
- Importing server-only modules (database, secrets) into `+page.ts` or components.
- Duplicating the same fetch in `+page.svelte` and `+page.server.ts`.
- Deep component nesting and duplicated code; remove unused components and routes.
## Example
```svelte
<!-- src/lib/components/HelloWorld.svelte -->
<script lang="ts">
let { name }: { name: string } = $props();
</script>
<h1>Hello, {name}!</h1>
```
```ts
// src/routes/users/+page.server.ts
import type { PageServerLoad } from './$types';
import { listUsers } from '$lib/server/users';
export const load: PageServerLoad = async () => {
return { users: await listUsers() };
};
```
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
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.
Cursor
coding
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A SvelteKit 2 app keeps layouts in src/lib/layouts, calls its own /api/users endpoint from components with node-fetch, and stores the logged-in user in a module-level writable.
Using the SvelteKit Architecture Rules, propose a restructure for this project tree and the users list page.
Layouts moved to +layout.svelte files (route groups if several layouts), the users page loads data in +page.server.ts from $lib/server, node-fetch removed, the user comes from event.locals via hooks.server.ts and layout load instead of a module store, with a file-move list.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/sveltekit-architecture-rules.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "SvelteKit Architecture Rules project conventions"
globs: "**/*"
alwaysApply: false
---
Apply these rules when structuring a SvelteKit application.
## Principles
- Keep each component and module focused on one responsibility; extract reusable UI into `src/lib/components`.
- Keep data loading and mutations on the server side of a route (`+page.server.ts`, `+layout.server.ts`, `+server.ts`) and pass plain data to components.
## Directory structure
```
src/
├── lib/
│ ├── components/ # reusable components (PascalCase.svelte)
│ ├── server/ # server-only code: db, auth, secrets (cannot be imported by client code)
│ └── state/ # shared client state (*.svelte.ts)
├── routes/
│ ├── +layout.svelte # layouts live in routes, next to the pages they wrap
│ ├── +page.svelte
│ ├── users/[id]/
│ │ ├── +page.svelte
│ │ └── +page.server.ts
│ └── api/users/+server.ts # JSON endpoints
├── hooks.server.ts # auth/session → event.locals
└── app.d.ts # App.Locals, App.PageData, App.Error types
```
- Routes are directories: a dynamic page is `src/routes/users/[id]/+page.svelte`, not `users/[id].svelte`.
- Layouts are `+layout.svelte` files in the route tree; use route groups such as `(app)` and `(marketing)` for different layouts without changing URLs.
- Put database clients, secrets and auth helpers in `$lib/server` or `*.server.ts` files; SvelteKit refuses to bundle them into client code.
## Naming
- Component files use PascalCase (`UserCard.svelte`); other modules follow the project's convention (kebab-case or camelCase), consistently.
- Variables and functions use camelCase; types and interfaces PascalCase.
## Types
- Use the generated `./$types` (`PageServerLoad`, `PageProps`, `Actions`, `RequestHandler`) for load functions, actions and endpoints, and declare `App.Locals` in `app.d.ts`.
## Data and API access
- Load page data in `load` functions and use the `fetch` passed to `load` (it forwards cookies and works during server rendering). Do not import `node-fetch`; modern Node and browsers provide `fetch`.
- Use `+server.ts` endpoints for JSON consumed by other clients; pages in the same app can read data from `load` directly instead of calling their own API.
- Use form actions for mutations from pages so forms work without JavaScript and progressively enhance with `use:enhance`.
## State
- Component-local state stays in the component (`$state` in Svelte 5).
- Shared client state lives in `.svelte.ts` modules or stores. On the server, modules are shared across all requests, so never keep per-user data (session, cart) in module-level state; use `event.locals`, `load` data or component context.
## Common pitfalls
- Importing server-only modules (database, secrets) into `+page.ts` or components.
- Duplicating the same fetch in `+page.svelte` and `+page.server.ts`.
- Deep component nesting and duplicated code; remove unused components and routes.
## Example
```svelte
<!-- src/lib/components/HelloWorld.svelte -->
<script lang="ts">
let { name }: { name: string } = $props();
</script>
<h1>Hello, {name}!</h1>
```
```ts
// src/routes/users/+page.server.ts
import type { PageServerLoad } from './$types';
import { listUsers } from '$lib/server/users';
export const load: PageServerLoad = async () => {
return { users: await listUsers() };
};
```
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.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
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.