Next.js Cursor Rules: Server Components, Routing, Fetching
Next.js cursor rules enforce server-first patterns, file routing, components, and efficient data fetching. Context-aware practices for maintainable code
Overview
Professional cursor rules for Next.js that guide server-first development, file-based routing, and clean separation between server and client components. These rules help AI assistants generate context-aware, maintainable code.
Note:
Focus on server components, data fetching strategies, route handlers, and clear context in code generation.
Rules Configuration
.cursor/rules/nextjs.mdc
---
description: Enforces best practices for Next.js development, focusing on context-aware code generation, server and client components, routing, and data fetching. Provides comprehensive guidelines for writing clean, efficient Next.js code with proper context.
globs: **/*.{js,jsx,ts,tsx}
---
# Next.js Best Practices
You are an expert in Next.js development and related web technologies.
You understand modern Next.js development practices, architectural patterns, and the importance of providing complete context in code generation.
## Next.js-Specific Rules
Cursor rules in Next.js provide intelligent navigation and manipulation capabilities designed specifically for Next.js development. These rules help you work more efficiently with server components, client components, and file-based routing.
### Component Navigation
- Navigate between server and client components
- Jump to page and layout definitions
- Smart navigation between route segments
- Quick access to component imports
- Navigate through data fetching functions
- Traverse component hierarchy
### Smart Selection
- Select component boundaries
- Expand selection to include metadata
- Smart server/client component selection
- Route handler selection
- Style module selection
- Select data fetching blocks
### Code Manipulation
- Quick component insertion
- Automated metadata handling
- Route configuration management
- Server/client directive optimization
- Style module configuration
- Framework component integration
## Best Practices
- Use server-first approach
- Leverage static/dynamic patterns
- Utilize route handlers effectively
- Navigate route tree efficiently
- Optimize component rendering
- Maintain server/client separation
## Examples
```jsx
// app/blog/[slug]/page.tsx
import { notFound } from 'next/navigation';
async function getPost(slug) {
const res = await fetch(`https://api.example.com/posts/${slug}`);
if (!res.ok) return undefined;
return res.json();
}
export default async function BlogPost({ params }) {
const post = await getPost(params.slug);
if (!post) {
notFound();
}
return (
<article className="blog-post">
<h1>{post.title}</h1>
<div>{post.content}</div>
</article>
);
}
// app/components/Counter.tsx
'use client';
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}
``
## Configuration
Customize Next.js-specific cursor rules in your settings:
```json
{
"nextjs.cursorRules": {
"serverComponentNavigation": true,
"clientComponentNavigation": true,
"routeHandling": true,
"dataFetchingSupport": true,
"layoutNavigation": true
}
}
Key Features
Server-First
Prefer server components and data fetching on the server
File-Based Routing
Clean route organization with layouts and nested segments
Data Fetching
Use fetch, caching, revalidation, and route handlers appropriately
Server/Client Separation
Only add client directives when interaction is required
Performance
Leverage static generation, ISR, and optimized assets
Testing Ready
Test critical routes and components with clear fixtures
Installation
Choose Your IDE
Select the appropriate file path based on your development environment.
Create the Rules File
Create the cursor rules file in your project using the tabs below.
.cursor/rules/nextjs.mdc
Add the Rules Configuration
Copy the configuration markdown above into the file.
Start Building
Your AI assistant will follow Next.js best practices automatically.
Use Cases
Content Sites
SSG/ISR with server components and clean routing
Dashboards
Hybrid rendering with authenticated server routes
API Routes
Route handlers for secure, efficient server logic
E-commerce
Product pages with ISR and optimized data fetching
Related Articles
Related Articles
PHP Cursor Rules: AI-Powered Development Best Practices
Cursor rules for PHP development enforcing PSR-12 standards, modern PHP 8+ features, and clean code principles with AI assistance for production-ready code.
Swift Cursor Rules: iOS Development Architecture & Testing
Swift cursor rules for iOS covering architecture, SwiftUI/UIKit, state management, async/await, testing with XCTest and XCUITest, performance-aware code generation.
Framework-Specific Cursor Rules for Web and Mobile Development
Discover cursor rules and best practices for popular frameworks including React, Vue, Angular, Next.js, Flutter, React Native, and more for efficient development.