Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+239 -299
View File
@@ -1,306 +1,246 @@
---
description: This rule provides comprehensive best practices and coding standards for SvelteKit development, covering code structure, performance, security, testing, and common pitfalls. It aims to improve code quality, maintainability, and overall project health.
globs: *.svelte
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 provides comprehensive best practices for developing SvelteKit applications. It covers various aspects, including code organization, performance considerations, security measures, and common pitfalls to avoid.
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
### 1.1. Directory Structure Best Practices
* **`src/lib`**: Reusable components, utilities, and stores. Organize by feature or domain.
* **`src/routes`**: Defines the application's routes. Each directory represents a route segment.
* **`src/routes/+page.svelte`**: Represents a page component for a route.
* **`src/routes/+layout.svelte`**: Layout component that wraps the page. Useful for consistent UI elements.
* **`src/routes/+page.server.ts`**: Server-side code for handling data fetching and form submissions for a page.
* **`src/routes/+layout.server.ts`**: Server-side code for layout data, loaded before any +page.server.ts data.
* **`src/hooks.server.ts`**: Handle server-side requests and responses. Useful for authentication and session management.
* **`static`**: Static assets like images, fonts, and other files served directly.
* **`tests`**: Directory for all tests, mirroring the source structure for clarity.
Example:
src/
├── lib/
│ ├── components/
│ │ ├── Button.svelte
│ │ └── Card.svelte
│ ├── utils/
│ │ ├── api.ts
│ │ └── helpers.ts
│ └── stores/
│ └── user.ts
├── routes/
│ ├── +
│ │ └── page.svelte
│ ├── about/
│ │ ├── +
│ │ │ └── page.svelte
│ │ └── +page.server.ts
│ └── blog/
│ ├── [slug]/
│ │ ├── +
│ │ │ └── page.svelte
│ │ └── +page.server.ts
│ └── +
│ └── page.svelte
├── hooks.server.ts
└── app.d.ts
### 1.2. File Naming Conventions
* **Components**: PascalCase (e.g., `MyComponent.svelte`).
* **Utility functions**: camelCase (e.g., `formatDate.ts`).
* **Stores**: camelCase (e.g., `userStore.ts`).
* **Route files**: Follow SvelteKit's routing conventions (`+page.svelte`, `+layout.svelte`, etc.).
### 1.3. Module Organization
* Group related functionality into modules.
* Use `src/lib` for reusable modules.
* Employ clear and descriptive module names.
* Leverage SvelteKit's `$lib` alias for importing modules from `src/lib`.
### 1.4. Component Architecture
* **Atomic Design**: Break down UI into small, reusable components (atoms, molecules, organisms, templates, pages).
* **Component Composition**: Compose complex UIs from simpler components.
* **Props and Events**: Use props for data input and events for component output.
* **Slots**: Allow parent components to inject content into child components.
### 1.5. Code Splitting Strategies
* **Dynamic Imports**: Use dynamic imports (`import()`) to load modules on demand.
* **Route-Based Splitting**: SvelteKit automatically splits code based on routes.
* **Component-Level Splitting**: Split large components into smaller chunks that can be loaded independently.
* **Lazy Loading**: Load non-critical components or modules only when needed.
## 2. Common Patterns and Anti-patterns
### 2.1. Design Patterns Specific to SvelteKit
* **Form Actions**: Use SvelteKit's form actions for handling form submissions on the server.
* **Load Functions**: Utilize `load` functions in `+page.server.ts` and `+layout.server.ts` for data fetching.
* **Stores for State Management**: Employ Svelte stores for managing application state.
* **Server-Side Rendering (SSR)**: Leverage SSR for improved SEO and initial load performance.
* **Progressive Enhancement**: Design applications to work even with JavaScript disabled.
### 2.2. Recommended Approaches for Common Tasks
* **Data Fetching**: Use `fetch` API or libraries like `axios` within `load` functions.
* **Form Handling**: Use SvelteKit's form actions for server-side validation and processing.
* **Authentication**: Implement authentication using libraries like `lucia-auth` or integrate with OAuth providers.
* **Authorization**: Implement role-based access control using stores or server-side checks.
* **Error Handling**: Use `try...catch` blocks and SvelteKit's `handleError` hook for global error handling.
### 2.3. Anti-patterns and Code Smells to Avoid
* **Over-reliance on Global State**: Avoid using global stores for component-specific state.
* **Direct DOM Manipulation**: Avoid directly manipulating the DOM; use Svelte's reactivity system instead.
* **Large, Unmaintainable Components**: Break down large components into smaller, reusable ones.
* **Ignoring Accessibility**: Ensure components are accessible to users with disabilities (ARIA attributes, semantic HTML).
* **Unnecessary Re-renders**: Optimize components to avoid unnecessary re-renders.
### 2.4. State Management Best Practices
* **Use Svelte Stores**: Svelte stores are the recommended way to manage application state.
* **Divide stores according to feature**: Split stores based on logical domain or feature (e.g. UserStore, CartStore).
* **Readable and Writable Stores**: Use readable stores for derived state and writable stores for mutable state.
* **Custom Stores**: Create custom stores for complex state management logic.
### 2.5. Error Handling Patterns
* **`try...catch` Blocks**: Use `try...catch` blocks to handle errors within components and functions.
* **`handleError` Hook**: Implement the `handleError` hook in `src/hooks.server.ts` for global error handling.
* **Error Boundaries**: Use error boundaries to catch errors in component trees and display fallback UI.
* **Logging**: Log errors to a server for monitoring and debugging.
## 3. Performance Considerations
### 3.1. Optimization Techniques
* **Lazy Loading**: Load non-critical resources (images, components) only when needed.
* **Code Splitting**: Split code into smaller chunks to reduce initial load time.
* **Image Optimization**: Optimize images using tools like `squoosh` or `tinypng`.
* **Server-Side Rendering (SSR)**: Use SSR for improved initial load performance and SEO.
* **Caching**: Implement caching strategies for data and assets.
* **Preload critical assets**: use `<link rel='preload'>` to fetch critical assets earlier
### 3.2. Memory Management
* **Avoid Memory Leaks**: Properly clean up resources when components unmount.
* **Use WeakRefs**: Use `WeakRefs` for managing objects that might be garbage collected.
* **Minimize Object Creation**: Avoid creating unnecessary objects and arrays.
* **Properly unsubscrive from stores:** Properly unsubscribe to avoid re-renders when the component is unmounted.
### 3.3. Rendering Optimization
* **Tracked values**: Use the `$:` syntax to track dependencies of reactive statements.
* **Svelte Compiler**: Utilize Svelte's compiler to optimize component rendering.
* **Virtual DOM**: Svelte doesn't use a virtual DOM, so updates are generally very efficient.
* **Avoid Unnecessary Re-renders**: Use `{#each}` blocks with keys to optimize list rendering.
* **Debouncing and Throttling**: Use debouncing and throttling to limit the frequency of updates.
### 3.4. Bundle Size Optimization
* **Tree Shaking**: Ensure unused code is removed during the build process.
* **Minification**: Minify JavaScript and CSS files to reduce bundle size.
* **Compression**: Compress assets using gzip or Brotli.
* **Dependency Analysis**: Analyze dependencies to identify and remove unused libraries.
### 3.5. Lazy Loading Strategies
* **Intersection Observer**: Use the Intersection Observer API to detect when elements are visible and load them lazily.
* **Dynamic Imports**: Use dynamic imports to load components and modules on demand.
* **Placeholder UI**: Display a placeholder UI while resources are loading.
## 4. Security Best Practices
### 4.1. Common Vulnerabilities and How to Prevent Them
* **Cross-Site Scripting (XSS)**: Sanitize user input to prevent XSS attacks.
* **Cross-Site Request Forgery (CSRF)**: Use CSRF tokens to protect against CSRF attacks.
* **SQL Injection**: Use parameterized queries or ORMs to prevent SQL injection.
* **Authentication and Authorization**: Implement secure authentication and authorization mechanisms.
* **Insecure Direct Object References (IDOR)**: Implement proper access controls to prevent unauthorized access to resources.
### 4.2. Input Validation
* **Server-Side Validation**: Always validate user input on the server.
* **Client-Side Validation**: Use client-side validation for immediate feedback, but never rely on it solely.
* **Escape User Input**: Escape user input before rendering it in the DOM.
* **Use Validation Libraries**: Use libraries like `zod` or `yup` for input validation.
### 4.3. Authentication and Authorization Patterns
* **JWT (JSON Web Tokens)**: Use JWTs for authentication and authorization.
* **OAuth 2.0**: Integrate with OAuth providers like Google and Facebook for social login.
* **Role-Based Access Control (RBAC)**: Implement RBAC to control access to resources based on user roles.
* **Multi-Factor Authentication (MFA)**: Implement MFA for enhanced security.
### 4.4. Data Protection Strategies
* **Encryption**: Encrypt sensitive data at rest and in transit.
* **Hashing**: Hash passwords using strong hashing algorithms like bcrypt or Argon2.
* **Data Masking**: Mask sensitive data in logs and reports.
* **Regular Security Audits**: Conduct regular security audits to identify and address vulnerabilities.
### 4.5. Secure API Communication
* **HTTPS**: Use HTTPS for all API communication.
* **API Keys**: Protect API keys and secrets.
* **Rate Limiting**: Implement rate limiting to prevent abuse.
* **Input Validation**: Always validate input to your APIs.
* **Output Encoding**: Always encode output from your API to prevent injection attacks.
## 5. Testing Approaches
### 5.1. Unit Testing Strategies
* **Test Individual Components**: Write unit tests for individual components in isolation.
* **Test Utility Functions**: Write unit tests for utility functions and modules.
* **Use Mock Data**: Use mock data for testing components and functions.
* **Test Edge Cases**: Test edge cases and error conditions.
* **Utilize Svelte Testing Library**: Employ `svelte-testing-library` for testing Svelte components.
### 5.2. Integration Testing
* **Test Component Interactions**: Write integration tests to ensure components interact correctly.
* **Test Data Flow**: Test the flow of data between components and modules.
* **Test API Integrations**: Test integrations with APIs and external services.
### 5.3. End-to-End Testing
* **Simulate User Interactions**: Write end-to-end tests to simulate user interactions.
* **Test Full Workflows**: Test complete user workflows from start to finish.
* **Use Playwright or Cypress**: Use tools like Playwright or Cypress for end-to-end testing.
### 5.4. Test Organization
* **Mirror Source Structure**: Organize tests in a directory structure that mirrors the source code.
* **Use Descriptive Names**: Use descriptive names for test files and functions.
* **Separate Test Environments**: Use separate test environments for unit, integration, and end-to-end tests.
### 5.5. Mocking and Stubbing
* **Mock API Calls**: Mock API calls to isolate components during testing.
* **Stub External Dependencies**: Stub external dependencies to control their behavior.
* **Use Mocking Libraries**: Use libraries like `jest.mock` or `sinon` for mocking and stubbing.
## 6. Common Pitfalls and Gotchas
### 6.1. Frequent Mistakes Developers Make
* **Incorrectly Using Reactive Statements**: Not understanding how Svelte's reactivity system works.
* **Ignoring Accessibility**: Neglecting accessibility considerations when building components.
* **Over-complicating State Management**: Using complex state management solutions when simpler ones would suffice.
* **Not Handling Errors Properly**: Ignoring errors or not providing informative error messages.
* **Premature Optimization**: Optimizing code before it is necessary.
### 6.2. Edge Cases to Be Aware Of
* **Server-Side Rendering Differences**: Be aware of differences between server-side and client-side rendering.
* **Browser Compatibility**: Test applications in different browsers to ensure compatibility.
* **Network Latency**: Design applications to handle network latency and unreliable connections.
* **Memory Constraints**: Consider memory constraints when building large applications.
### 6.3. Version-Specific Issues
* **Breaking Changes**: Be aware of breaking changes in new versions of SvelteKit.
* **Deprecated Features**: Avoid using deprecated features.
* **Upgrade Guides**: Follow upgrade guides when migrating to new versions of SvelteKit.
### 6.4. Compatibility Concerns
* **Third-Party Libraries**: Ensure third-party libraries are compatible with SvelteKit.
* **Browser Support**: Check browser support for new features and APIs.
* **Node.js Version**: Ensure the Node.js version is compatible with SvelteKit.
### 6.5. Debugging Strategies
* **Browser Developer Tools**: Use browser developer tools for debugging JavaScript and CSS.
* **Svelte Devtools**: Use the Svelte Devtools extension for inspecting Svelte components.
* **Logging**: Use `console.log` statements for debugging.
* **Debuggers**: Use debuggers like VS Code's debugger for step-by-step debugging.
## 7. Tooling and Environment
### 7.1. Recommended Development Tools
* **VS Code**: VS Code is a popular code editor with excellent Svelte and TypeScript support.
* **Svelte Devtools**: The Svelte Devtools browser extension allows you to inspect Svelte components.
* **ESLint**: ESLint is a linter that helps enforce code style and best practices.
* **Prettier**: Prettier is a code formatter that automatically formats code to a consistent style.
* **TypeScript**: Using typescript is recommended for type safety.
### 7.2. Build Configuration
* **`svelte.config.js`**: Configure SvelteKit using the `svelte.config.js` file.
* **Vite**: SvelteKit uses Vite as its build tool.
* **Environment Variables**: Use environment variables for configuration settings.
* **Adapters**: Use adapters to deploy SvelteKit applications to different environments.
### 7.3. Linting and Formatting
* **ESLint**: Configure ESLint to enforce code style and best practices.
* **Prettier**: Configure Prettier to automatically format code.
* **Husky**: Use Husky to run linters and formatters before committing code.
* **Lint Staged**: Use lint-staged to run linters and formatters only on staged files.
### 7.4. Deployment Best Practices
* **Choose an Adapter**: Use an adapter like `@sveltejs/adapter-node` or `@sveltejs/adapter-static` to deploy the application.
* **Configure Environment Variables**: Configure environment variables for production environments.
* **Set Up HTTPS**: Set up HTTPS for secure communication.
* **Monitor the Application**: Monitor the application for errors and performance issues.
### 7.5. CI/CD Integration
* **Use a CI/CD Pipeline**: Use a CI/CD pipeline to automate the build, test, and deployment process.
* **Automated Tests**: Run automated tests in the CI/CD pipeline.
* **Automated Deployments**: Automate deployments to production environments.
* **Use tools like GitHub Actions, GitLab CI, or CircleCI**
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
<!-- src/lib/components/Button.svelte -->
<script lang="ts">
import type { HTMLButtonAttributes } from 'svelte/elements';
import type { Snippet } from 'svelte';
interface Props extends HTMLButtonAttributes {
variant?: 'primary' | 'secondary';
children: Snippet;
}
let { variant = 'primary', children, ...rest }: Props = $props();
</script>
<button class:primary={variant === 'primary'} class:secondary={variant === 'secondary'} {...rest}>
{@render children()}
</button>
<style>
button { /* ... base styles ... */ }
.primary { /* ... primary styles ... */ }
.secondary { /* ... secondary styles ... */ }
</style>
```
## 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
<script lang="ts">
import { writable } from 'svelte/store';
const count = writable(0);
</script>
<button on:click={() => count.update(n => n + 1)}>Count: {$count}</button>
```
✅ GOOD (Svelte 5 runes for local state):
```svelte
<script lang="ts">
let count = $state(0);
</script>
<button on:click={() => count++}>Count: {count}</button>
```
✅ 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
<!-- src/routes/+page.svelte -->
<script lang="ts">
import { counter } from '$lib/stores/counter';
</script>
<button on:click={() => counter.increment()}>Count: {counter.count}</button>
```
## 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
<!-- src/routes/blog/[slug]/+page.svelte -->
<script lang="ts">
import type { PageProps } from './$types';
let { data }: PageProps = $props();
</script>
<h1>{data.post.title}</h1>
<p>{data.post.content}</p>
```
**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
<div on:click={doSomething}>Click me</div>
```
✅ GOOD:
```svelte
<button on:click={doSomething}>Click me</button>
```
## 7. Testing Approaches
Implement a robust testing strategy.
**Rule**: Use Playwright for end-to-end tests in `tests/` and Vitest for unit tests, co-located with the code they test. Integrate `svelte-check`, ESLint, and Prettier into your CI pipeline.
```typescript
// src/lib/utils/math.test.ts (Vitest unit test)
import { expect, test } from 'vitest';
import { add } from './math';
test('add function sums numbers correctly', () => {
expect(add(1, 2)).toBe(3);
});
```
## 8. Common Pitfalls and Gotchas
Avoid common mistakes that lead to bugs or performance issues.
**Rule**: Do not import `$lib/server` modules into client-side code. SvelteKit will prevent this at build time, but catching it early saves time.
❌ BAD (in `+page.ts` or `+page.svelte`):
```typescript
import { serverOnlyFunction } from '$lib/server/utils'; // This will fail
```
✅ GOOD (in `+page.server.ts` or `+server.ts`):
```typescript
import { serverOnlyFunction } from '$lib/server/utils'; // Correct usage
```
**Rule**: Configure `tsconfig.json` correctly, extending SvelteKit's generated config. Specifically, ensure `verbatimModuleSyntax: true` and `isolatedModules: true`.
```json
// tsconfig.json
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"target": "ES2022",
"verbatimModuleSyntax": true,
"isolatedModules": true,
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "bundler",
"module": "ESNext",
"resolveJsonModule": true,
"allowJs": true,
"checkJs": true,
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"types": ["vitest/globals"] // If using Vitest
},
"include": ["src/**/*.d.ts", "src/**/*.ts", "src/**/*.js", "src/**/*.svelte"],
"exclude": ["node_modules"]
}
```