mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
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:
+315
-144
@@ -1,161 +1,332 @@
|
||||
---
|
||||
description: Enforces Vercel's recommended coding style, optimization strategies, and security best practices. This guide helps developers build performant, secure, and maintainable applications on the Vercel platform.
|
||||
globs: *.js,*.jsx,*.ts,*.tsx,*.md,*.mdx,*.json,*.yml,*.yaml,*.html,*.css,*.scss,*.sass
|
||||
description: This guide outlines definitive best practices for developing and deploying applications on Vercel, ensuring optimal performance, security, and cost-efficiency.
|
||||
globs: **/*
|
||||
---
|
||||
- Adhere to Vercel's Style Guide: Utilize linting and styling tools configured according to the Vercel Style Guide for consistent code formatting and style. Use `@file vercel_style_guide.mdc` to include style guide details.
|
||||
- Enforce Consistent Coding Style: Integrate ESLint, Prettier, and TypeScript to automatically enforce coding style and prevent stylistic inconsistencies. Refer to [Vercel's documentation](https://vercel.com/docs) for setup instructions.
|
||||
- Optimize Codebase Performance: Focus on optimizing the codebase for faster loading times and improved user experience. Includes optimizing cache, assets, and serverless functions.
|
||||
- Implement Conformance Checks: Utilize Vercel's Conformance tools to automatically check for performance, security, and code health issues.
|
||||
# vercel Best Practices
|
||||
|
||||
## 1. Code Organization and Structure:
|
||||
Vercel is the definitive platform for deploying modern web applications, especially those built with Next.js. To maximize its potential, adhere to these opinionated guidelines for performance, security, and maintainability.
|
||||
|
||||
- **Directory Structure:**
|
||||
- Adopt a clear and consistent directory structure (e.g., `components`, `pages`, `lib`, `api`, `styles`, `public`). Group related files logically to improve maintainability.
|
||||
- Use a `src` directory to encapsulate all source code, separating it from configuration files and other project assets.
|
||||
- **File Naming Conventions:**
|
||||
- Use descriptive and consistent file names (e.g., `Button.tsx`, `useUser.ts`, `api/products.js`).
|
||||
- Prefer camelCase for JavaScript/TypeScript files and kebab-case for CSS/SCSS files.
|
||||
- **Module Organization:**
|
||||
- Organize code into reusable modules or components. Favor small, focused modules with well-defined interfaces.
|
||||
- Use ES modules (`import`/`export`) for modularity and dependency management.
|
||||
- **Component Architecture:**
|
||||
- Employ a component-based architecture (e.g., using React, Vue, or Svelte) to build reusable UI elements.
|
||||
- Follow the principles of separation of concerns and single responsibility.
|
||||
- Consider using a design system or component library to maintain consistency across the application.
|
||||
- **Code Splitting Strategies:**
|
||||
- Implement code splitting to reduce the initial bundle size and improve loading times. Use dynamic imports for route-based or component-based splitting.
|
||||
- Analyze bundle size using tools like `webpack-bundle-analyzer` to identify large dependencies.
|
||||
## 1. Code Organization & Structure
|
||||
|
||||
## 2. Common Patterns and Anti-patterns:
|
||||
Organize your project to leverage Vercel's serverless functions and build optimizations.
|
||||
|
||||
- **Design Patterns Specific to Vercel:**
|
||||
- Serverless Functions: Use serverless functions for API endpoints and background tasks. Optimize function size and execution time to minimize cold starts.
|
||||
- Edge Functions: Use edge functions to perform operations closer to the user, reducing latency for geographically distributed users.
|
||||
- Incremental Static Regeneration (ISR): Use ISR to combine the benefits of static generation and server-side rendering.
|
||||
- **Recommended Approaches for Common Tasks:**
|
||||
- Data Fetching: Use `getStaticProps` or `getServerSideProps` in Next.js for data fetching in pages.
|
||||
- API Routes: Create API routes in the `pages/api` directory of a Next.js project for handling API requests.
|
||||
- Environment Variables: Use environment variables to store sensitive information and configuration settings.
|
||||
- **Anti-patterns and Code Smells to Avoid:**
|
||||
- Large components: Break down large components into smaller, more manageable pieces.
|
||||
- Deeply nested components: Avoid excessive nesting of components, which can make code harder to read and maintain.
|
||||
- Over-fetching data: Fetch only the data that is needed by a component.
|
||||
- Mutating state directly: Avoid mutating state directly, which can lead to unexpected behavior.
|
||||
- **State Management Best Practices:**
|
||||
- Choose a state management solution (e.g., Redux, Zustand, Recoil, React Context) that is appropriate for the application's complexity.
|
||||
- Follow recommended patterns for managing state (e.g., reducers, actions, selectors).
|
||||
- Avoid storing too much data in the global state.
|
||||
- **Error Handling Patterns:**
|
||||
- Implement comprehensive error handling throughout the application.
|
||||
- Use try-catch blocks to catch exceptions.
|
||||
- Log errors to a monitoring service.
|
||||
- Display user-friendly error messages.
|
||||
### 1.1. Serverless Functions in `api/`
|
||||
|
||||
## 3. Performance Considerations:
|
||||
All serverless functions must reside within the `api/` directory (or `app/api` for App Router). This is Vercel's convention for automatic API route detection.
|
||||
|
||||
- **Optimization Techniques:**
|
||||
- Minimize bundle size by removing unused code and dependencies (tree shaking).
|
||||
- Compress images and other assets.
|
||||
- Use a content delivery network (CDN) to serve static assets.
|
||||
- Optimize database queries.
|
||||
- Cache frequently accessed data.
|
||||
- **Memory Management:**
|
||||
- Avoid memory leaks by properly releasing resources.
|
||||
- Use garbage collection to reclaim unused memory.
|
||||
- Profile application memory usage to identify potential issues.
|
||||
- **Rendering Optimization:**
|
||||
- Use memoization techniques (e.g., `React.memo`, `useMemo`) to prevent unnecessary re-renders.
|
||||
- Virtualize long lists to improve rendering performance.
|
||||
- Use code splitting to reduce the initial bundle size.
|
||||
- **Bundle Size Optimization:**
|
||||
- Analyze bundle size with tools like `webpack-bundle-analyzer` or `source-map-explorer`.
|
||||
- Remove unused dependencies.
|
||||
- Minify code.
|
||||
- Compress code with gzip or Brotli.
|
||||
- **Lazy Loading Strategies:**
|
||||
- Implement lazy loading for images, components, and routes.
|
||||
- Use the `IntersectionObserver` API to detect when an element is visible in the viewport.
|
||||
❌ **BAD: Mixing serverless logic outside `api/`**
|
||||
```javascript
|
||||
// pages/products.js
|
||||
export default function Products() {
|
||||
// ... client-side logic
|
||||
}
|
||||
|
||||
## 4. Security Best Practices:
|
||||
// In some_utility.js
|
||||
export async function fetchProductsData() {
|
||||
// This function contains server-side logic and heavy dependencies
|
||||
// but is not in a dedicated serverless function file.
|
||||
const data = await expensiveDatabaseCall();
|
||||
return data;
|
||||
}
|
||||
```
|
||||
|
||||
- **Common Vulnerabilities and How to Prevent Them:**
|
||||
- Cross-site scripting (XSS): Sanitize user input to prevent XSS attacks. Use a library like DOMPurify.
|
||||
- Cross-site request forgery (CSRF): Use CSRF tokens to protect against CSRF attacks.
|
||||
- SQL injection: Use parameterized queries or an ORM to prevent SQL injection attacks.
|
||||
- Authentication and authorization vulnerabilities: Implement strong authentication and authorization mechanisms.
|
||||
- **Input Validation:**
|
||||
- Validate all user input on both the client and server sides.
|
||||
- Use a validation library to simplify the validation process.
|
||||
- **Authentication and Authorization Patterns:**
|
||||
- Use a secure authentication protocol (e.g., OAuth 2.0, JWT).
|
||||
- Implement role-based access control (RBAC) to restrict access to sensitive resources.
|
||||
- **Data Protection Strategies:**
|
||||
- Encrypt sensitive data at rest and in transit.
|
||||
- Use HTTPS to secure communication between the client and server.
|
||||
- Protect against data breaches by implementing appropriate security measures.
|
||||
- **Secure API Communication:**
|
||||
- Use HTTPS to encrypt API traffic.
|
||||
- Authenticate and authorize API requests.
|
||||
- Limit API rate to prevent abuse.
|
||||
✅ **GOOD: Centralized serverless functions**
|
||||
```javascript
|
||||
// pages/products.js (or app/products/page.tsx)
|
||||
import useSWR from 'swr';
|
||||
|
||||
## 5. Testing Approaches:
|
||||
export default function Products() {
|
||||
const { data, error } = useSWR('/api/products', fetcher);
|
||||
// ... client-side rendering
|
||||
}
|
||||
|
||||
- **Unit Testing Strategies:**
|
||||
- Write unit tests for individual components, functions, and modules.
|
||||
- Use a unit testing framework (e.g., Jest, Mocha, Jasmine).
|
||||
- Mock external dependencies.
|
||||
- **Integration Testing:**
|
||||
- Write integration tests to verify that different parts of the application work together correctly.
|
||||
- Test interactions between components, modules, and APIs.
|
||||
- **End-to-End Testing:**
|
||||
- Write end-to-end tests to simulate user interactions and verify that the application works as expected.
|
||||
- Use an end-to-end testing framework (e.g., Cypress, Playwright).
|
||||
- **Test Organization:**
|
||||
- Organize tests into separate directories based on the type of test (e.g., unit, integration, e2e).
|
||||
- Use descriptive test names.
|
||||
- **Mocking and Stubbing:**
|
||||
- Use mocking and stubbing to isolate components and functions during testing.
|
||||
- Mock external dependencies to avoid making real API calls.
|
||||
// api/products.js (or app/api/products/route.ts)
|
||||
import { NextResponse } from 'next/server';
|
||||
import { expensiveDatabaseCall } from '@/lib/db'; // Separate DB logic
|
||||
|
||||
## 6. Common Pitfalls and Gotchas:
|
||||
export const runtime = 'edge'; // Use Edge Runtime for speed
|
||||
|
||||
- **Frequent Mistakes Developers Make:**
|
||||
- Over-reliance on client-side rendering.
|
||||
- Neglecting performance optimization.
|
||||
- Ignoring security vulnerabilities.
|
||||
- Not writing enough tests.
|
||||
- **Edge Cases to Be Aware Of:**
|
||||
- Handling errors gracefully.
|
||||
- Supporting different browsers and devices.
|
||||
- Dealing with slow network connections.
|
||||
- **Version-Specific Issues:**
|
||||
- Be aware of breaking changes in Vercel and its dependencies.
|
||||
- Test application thoroughly after upgrading Vercel or any dependencies.
|
||||
- **Compatibility Concerns:**
|
||||
- Ensure that the application is compatible with different browsers, devices, and operating systems.
|
||||
- **Debugging Strategies:**
|
||||
- Use browser developer tools to debug client-side code.
|
||||
- Use server-side logging to debug server-side code.
|
||||
- Use a debugger to step through code and inspect variables.
|
||||
export async function GET() {
|
||||
try {
|
||||
const data = await expensiveDatabaseCall();
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Failed to fetch products' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 7. Tooling and Environment:
|
||||
### 1.2. Monorepo Caching with Turborepo
|
||||
|
||||
- **Recommended Development Tools:**
|
||||
- VS Code with extensions for ESLint, Prettier, and TypeScript.
|
||||
- Chrome DevTools for debugging.
|
||||
- Vercel CLI for deploying and managing applications.
|
||||
- **Build Configuration:**
|
||||
- Configure build scripts to optimize code and assets.
|
||||
- Use environment variables to configure the build process.
|
||||
- **Linting and Formatting:**
|
||||
- Use ESLint and Prettier to enforce consistent code style.
|
||||
- Configure linting and formatting rules to match the Vercel Style Guide.
|
||||
- **Deployment Best Practices:**
|
||||
- Deploy application to Vercel using the Vercel CLI or Git integration.
|
||||
- Configure environment variables for production.
|
||||
- Monitor application performance and logs.
|
||||
- **CI/CD Integration:**
|
||||
- Integrate with a CI/CD pipeline (e.g., GitHub Actions, CircleCI) to automate the build, test, and deployment process.
|
||||
- Run tests and linters as part of the CI/CD pipeline.
|
||||
For monorepos, always integrate Turborepo to enable intelligent caching and prevent redundant builds. Vercel automatically detects and optimizes for Turborepo.
|
||||
|
||||
@file vercel_style_guide.mdc
|
||||
❌ **BAD: Monorepo without Turborepo caching**
|
||||
```bash
|
||||
# In a large monorepo, every Vercel build will re-build all packages
|
||||
# even if only one has changed.
|
||||
npm run build # Rebuilds everything
|
||||
```
|
||||
|
||||
✅ **GOOD: Turborepo for efficient builds**
|
||||
```json
|
||||
// package.json (root)
|
||||
{
|
||||
"name": "my-monorepo",
|
||||
"private": true,
|
||||
"workspaces": ["apps/*", "packages/*"],
|
||||
"scripts": {
|
||||
"build": "turbo run build",
|
||||
"dev": "turbo run dev --parallel"
|
||||
},
|
||||
"devDependencies": {
|
||||
"turbo": "latest"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Common Patterns & Anti-patterns
|
||||
|
||||
Leverage Vercel's strengths, particularly Edge Functions and Fluid Compute, while avoiding common performance traps.
|
||||
|
||||
### 2.1. Prefer Edge Runtime & Fluid Compute
|
||||
|
||||
For serverless functions, prioritize the Edge Runtime and ensure Fluid Compute is enabled for automatic cold start optimizations and cost efficiency.
|
||||
|
||||
❌ **BAD: Default Node.js runtime for latency-sensitive functions**
|
||||
```javascript
|
||||
// api/heavy-computation.js
|
||||
// This will run in a traditional Node.js Lambda, potentially incurring cold starts.
|
||||
export default async function handler(req, res) {
|
||||
// ... heavy logic
|
||||
}
|
||||
```
|
||||
|
||||
✅ **GOOD: Edge Runtime with Fluid Compute**
|
||||
```javascript
|
||||
// api/fast-response.js
|
||||
export const runtime = 'edge'; // Explicitly set Edge Runtime
|
||||
export const preferredRegion = 'iad1'; // Choose region closest to users/data
|
||||
|
||||
export default async function handler(req) {
|
||||
// Fluid Compute automatically handles cold starts and resource scaling.
|
||||
// ... lightweight, latency-sensitive logic
|
||||
return new Response('Hello from the Edge!');
|
||||
}
|
||||
```
|
||||
|
||||
### 2.2. Static Exports for Purely Static Sites
|
||||
|
||||
If your Next.js application is purely static (no server-side rendering or API routes), use `output: 'export'` for maximum performance and cost savings.
|
||||
|
||||
❌ **BAD: Deploying a static site without `output: 'export'`**
|
||||
```javascript
|
||||
// next.config.js
|
||||
const nextConfig = {}; // Default configuration
|
||||
module.exports = nextConfig; // Still deploys as a hybrid app, incurring serverless overhead
|
||||
```
|
||||
|
||||
✅ **GOOD: Static export for static sites**
|
||||
```javascript
|
||||
// next.config.js
|
||||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {
|
||||
output: 'export', // Generates static HTML, CSS, JS
|
||||
images: {
|
||||
unoptimized: true, // Required for static export if using Next/image
|
||||
},
|
||||
trailingSlash: true, // Consistent URLs for static exports
|
||||
};
|
||||
module.exports = nextConfig;
|
||||
```
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
Vercel excels at performance. Ensure your code is optimized to take full advantage.
|
||||
|
||||
### 3.1. Minimize Serverless Function Bundle Size
|
||||
|
||||
Large dependencies in serverless functions directly increase cold start times. Keep functions lean.
|
||||
|
||||
❌ **BAD: Importing large libraries into serverless functions**
|
||||
```javascript
|
||||
// api/data-processor.js
|
||||
import { giantLibrary } from 'giant-library'; // ~5MB dependency
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const result = giantLibrary.process(req.body);
|
||||
res.status(200).json(result);
|
||||
}
|
||||
```
|
||||
|
||||
✅ **GOOD: Keep functions small and use dynamic imports**
|
||||
```javascript
|
||||
// api/data-processor.js
|
||||
// Only import what's absolutely necessary.
|
||||
// Consider moving heavy processing to a dedicated service or a different function.
|
||||
|
||||
export default async function handler(req, res) {
|
||||
// If a large dependency is unavoidable, consider dynamic import for divergent paths
|
||||
if (req.query.type === 'special') {
|
||||
const { giantLibrary } = await import('giant-library');
|
||||
const result = giantLibrary.process(req.body);
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
// ... lighter logic
|
||||
return res.status(200).json({ message: 'Processed lightly' });
|
||||
}
|
||||
```
|
||||
|
||||
### 3.2. Implement Caching Headers
|
||||
|
||||
Utilize `Cache-Control` headers for API routes and static assets to leverage Vercel's global CDN.
|
||||
|
||||
❌ **BAD: No caching headers for static data**
|
||||
```javascript
|
||||
// api/cached-data.js
|
||||
export default async function handler(req, res) {
|
||||
const data = await fetchData();
|
||||
res.status(200).json(data); // Default no-cache
|
||||
}
|
||||
```
|
||||
|
||||
✅ **GOOD: Aggressive caching for static/immutable data**
|
||||
```javascript
|
||||
// api/cached-data.js
|
||||
export default async function handler(req, res) {
|
||||
const data = await fetchData();
|
||||
res.setHeader('Cache-Control', 's-maxage=3600, stale-while-revalidate'); // Cache for 1 hour at edge
|
||||
res.status(200).json(data);
|
||||
}
|
||||
```
|
||||
|
||||
### 3.3. Optimize Images with Next.js Image Component
|
||||
|
||||
Always use the Next.js `Image` component for image optimization. Vercel handles the resizing and serving via its CDN. Be aware of the image optimization pricing for older teams.
|
||||
|
||||
❌ **BAD: Standard `<img>` tags for dynamic images**
|
||||
```html
|
||||
<img src="/my-image.jpg" alt="My Image" width="800" height="600" />
|
||||
```
|
||||
|
||||
✅ **GOOD: Next.js Image component for optimized images**
|
||||
```jsx
|
||||
import Image from 'next/image';
|
||||
|
||||
<Image
|
||||
src="/my-image.jpg"
|
||||
alt="My Image"
|
||||
width={800}
|
||||
height={600}
|
||||
quality={80} // Adjust quality as needed
|
||||
priority // For LCP images
|
||||
/>
|
||||
```
|
||||
|
||||
## 4. Common Pitfalls & Gotchas
|
||||
|
||||
Avoid common mistakes that lead to unexpected behavior or security vulnerabilities.
|
||||
|
||||
### 4.1. Environment Variables Scoping
|
||||
|
||||
Always scope environment variables correctly (Production, Preview, Development) in the Vercel dashboard. Never commit sensitive keys to your repository.
|
||||
|
||||
❌ **BAD: Hardcoding API keys or incorrect scoping**
|
||||
```javascript
|
||||
// api/external-service.js
|
||||
const API_KEY = 'sk_hardcoded_key'; // NEVER do this!
|
||||
// ... or setting a variable only for Production when needed in Preview
|
||||
```
|
||||
|
||||
✅ **GOOD: Use Vercel's UI for environment variables**
|
||||
```javascript
|
||||
// api/external-service.js
|
||||
// Access via process.env
|
||||
const API_KEY = process.env.EXTERNAL_SERVICE_API_KEY;
|
||||
|
||||
// In Vercel Dashboard: Project Settings -> Environment Variables
|
||||
// Add EXTERNAL_SERVICE_API_KEY for Development, Preview, and Production environments.
|
||||
```
|
||||
|
||||
### 4.2. Missing Security Headers & CSP
|
||||
|
||||
Implement robust security headers, especially a Content Security Policy (CSP), to protect against XSS and other attacks.
|
||||
|
||||
❌ **BAD: Relying on default browser security**
|
||||
```javascript
|
||||
// next.config.js
|
||||
// No security headers configured.
|
||||
const nextConfig = {};
|
||||
module.exports = nextConfig;
|
||||
```
|
||||
|
||||
✅ **GOOD: Configure security headers in `next.config.js`**
|
||||
```javascript
|
||||
// next.config.js
|
||||
const nextConfig = {
|
||||
async headers() {
|
||||
return [
|
||||
{
|
||||
source: '/:path*',
|
||||
headers: [
|
||||
{
|
||||
key: 'X-Content-Type-Options',
|
||||
value: 'nosniff',
|
||||
},
|
||||
{
|
||||
key: 'X-Frame-Options',
|
||||
value: 'DENY',
|
||||
},
|
||||
{
|
||||
key: 'X-XSS-Protection',
|
||||
value: '1; mode=block',
|
||||
},
|
||||
{
|
||||
key: 'Content-Security-Policy',
|
||||
value: "default-src 'self'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self';",
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
},
|
||||
};
|
||||
module.exports = nextConfig;
|
||||
```
|
||||
|
||||
## 5. Deployment Workflow & CI/CD
|
||||
|
||||
Streamline your deployment process using Vercel's native Git integration and CLI.
|
||||
|
||||
### 5.1. Leverage Preview Deployments
|
||||
|
||||
Always use Vercel's automatic Preview Deployments for every pull request. This enables rapid feedback and testing before merging to `main`.
|
||||
|
||||
❌ **BAD: Merging directly to `main` without previewing**
|
||||
```bash
|
||||
git push origin main # Directly pushes to production, risking bugs
|
||||
```
|
||||
|
||||
✅ **GOOD: Review changes on a preview deployment**
|
||||
```bash
|
||||
# 1. Create a feature branch
|
||||
git checkout -b feature/new-feature
|
||||
# 2. Push to GitHub, Vercel creates a preview deployment
|
||||
git push origin feature/new-feature
|
||||
# 3. Open a Pull Request, share the Vercel preview URL with teammates for review.
|
||||
# 4. Once approved and tested on preview, merge PR to `main`.
|
||||
# Vercel automatically deploys to production.
|
||||
```
|
||||
|
||||
### 5.2. Use `vercel --prod` for Manual Production Deploys
|
||||
|
||||
While Git integration is primary, use `vercel --prod` via CLI for controlled production deployments, especially in CI/CD pipelines.
|
||||
|
||||
❌ **BAD: Deploying to production via `vercel` without `--prod`**
|
||||
```bash
|
||||
vercel # Deploys to a preview URL, not production
|
||||
```
|
||||
|
||||
✅ **GOOD: Explicit production deployment**
|
||||
```bash
|
||||
# In your CI/CD pipeline after tests pass:
|
||||
vercel --prod --token=$VERCEL_TOKEN # Deploys the current branch to production
|
||||
```
|
||||
Reference in New Issue
Block a user