--- name: sveltekit description: Expert guidance for SvelteKit development with TypeScript, Tailwind CSS, SSR/SSG, and performance optimization best practices --- # SvelteKit Development You are an expert in SvelteKit, TypeScript, Tailwind CSS, and modern web application development. ## Key Principles - Write concise, technical SvelteKit code with accurate TypeScript examples - Use functional and declarative programming patterns; avoid classes - Prefer iteration and modularization over code duplication - Use descriptive variable names with auxiliary verbs (isLoading, hasError) - Structure files: component logic, markup, styles, helpers, types ## Project Structure ``` src/ ├── lib/ │ ├── components/ # Reusable Svelte components │ ├── server/ # Server-only utilities │ ├── stores/ # Svelte stores │ └── utils/ # Shared utilities ├── routes/ │ ├── +layout.svelte # Root layout │ ├── +page.svelte # Home page │ └── api/ # API routes ├── app.html # HTML template └── app.css # Global styles ``` ## Component Development ### Script Setup with TypeScript ```svelte ``` ### Props and Events ```svelte ``` ## Routing ### File-Based Routes ``` routes/ ├── +page.svelte # / ├── about/+page.svelte # /about ├── blog/ │ ├── +page.svelte # /blog │ └── [slug]/ │ └── +page.svelte # /blog/:slug ``` ### Dynamic Routes ```svelte
{description}
{$page.error?.message}
``` ```typescript // +page.server.ts import { error } from '@sveltejs/kit'; export const load: PageServerLoad = async ({ params }) => { const post = await getPost(params.slug); if (!post) { throw error(404, 'Post not found'); } return { post }; }; ``` ## Performance Optimization - Use `{#key}` blocks for component recreation - Implement lazy loading with dynamic imports - Use `$effect.pre` for DOM measurements - Optimize images with `@sveltejs/enhanced-img` - Prefetch links with `data-sveltekit-preload-data` ## Testing ```typescript // Component testing with Vitest import { render, screen } from '@testing-library/svelte'; import { expect, test } from 'vitest'; import Button from './Button.svelte'; test('renders button with text', () => { render(Button, { props: { label: 'Click me' } }); expect(screen.getByRole('button')).toHaveTextContent('Click me'); }); ``` ## Accessibility - Use semantic HTML elements - Add ARIA labels where needed - Ensure keyboard navigation - Test with screen readers - Maintain focus management