--- description: Definitive guidelines for building scalable, performant, and maintainable SvelteKit applications using modern best practices, TypeScript, and Svelte 5 runes. globs: **/*.{js,jsx,ts,tsx,svelte} --- # SvelteKit Best Practices This guide outlines the definitive best practices for developing SvelteKit applications. Adhere to these principles for consistent, high-quality, and performant code. ## 1. Code Organization and Structure Maintain a clean, scalable project structure. * **`src/routes/`**: All page and API endpoints. Co-locate `+page.svelte`, `+page.ts`, `+layout.svelte`, `+error.svelte`, `+server.ts` files. * **`src/lib/`**: Reusable utilities, components, and client-side logic. Imported via `$lib`. * **`src/lib/server/`**: Server-only modules. Imported via `$lib/server`. SvelteKit prevents client-side import. * **`src/params/`**: Custom route matchers. * **`static/`**: Static assets (e.g., `favicon.png`, `robots.txt`). * **`tests/`**: End-to-end (E2E) tests (e.g., Playwright). Unit tests can be co-located with `src/`. **Rule**: Use `+` prefixed filenames for SvelteKit's routing and special files. ❌ BAD: ``` src/routes/users/index.svelte src/routes/api/users.ts ``` ✅ GOOD: ```svelte src/routes/users/+page.svelte src/routes/api/users/+server.ts ``` ## 2. Component Architecture Design components for reusability and clarity. **Rule**: Use `lang="ts"` for TypeScript in Svelte components and leverage `$props()` for type-safe props. ```svelte ``` ## 3. State Management Prioritize Svelte 5 runes for local reactivity and classes for complex, encapsulated state. **Rule**: Use `$state()` for local, reactive component state. For complex logic or shared state, encapsulate it within a class. ❌ BAD (Old Svelte 4 store for local state): ```svelte ``` ✅ GOOD (Svelte 5 runes for local state): ```svelte ``` ✅ GOOD (Class for complex/shared state): ```typescript // src/lib/stores/counter.ts class CounterStore { count = $state(0); increment() { this.count++; } reset() { this.count = 0; } } export const counter = new CounterStore(); // Singleton instance ``` ```svelte ``` ## 4. Routing and Data Fetching Leverage SvelteKit's file-system routing and `load` functions. **Rule**: Use `+page.ts` for universal `load` functions (SSR + CSR) and `+page.server.ts` for server-only `load` functions or form actions. Always type `load` functions with `$types`. ```typescript // src/routes/blog/[slug]/+page.ts import { error } from '@sveltejs/kit'; import type { PageLoad } from './$types'; export const load: PageLoad = async ({ params, fetch }) => { const res = await fetch(`/api/blog/${params.slug}`); if (!res.ok) { error(res.status, 'Could not fetch blog post.'); } const post = await res.json(); return { post }; }; ``` ```svelte
{data.post.content}
``` **Rule**: For API endpoints, use `+server.ts` files. ```typescript // src/routes/api/blog/[slug]/+server.ts import { json, error } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; export const GET: RequestHandler = async ({ params }) => { // In a real app, fetch from DB if (params.slug === 'hello-world') { return json({ title: 'Hello World', content: 'Welcome to our blog!' }); } error(404, 'Not Found'); }; ``` ## 5. Performance Considerations Optimize for speed and efficiency. **Rule**: Prerender static routes by exporting `export const prerender = true` in `+page.ts` or `+layout.ts`. Use `@sveltejs/enhanced-img` for image optimization. ```typescript // src/routes/about/+page.ts export const prerender = true; // This page will be statically generated at build time ``` **Rule**: Deploy to platforms like Vercel for zero-config serverless functions and automatic optimizations. ## 6. Accessibility Build inclusive user interfaces. **Rule**: Always use semantic HTML, ensure keyboard navigability, and test with screen readers. ❌ BAD: ```svelte