---
description: This rule explains Next.js conventions and best practices for fullstack development.
globs: **/*.js,**/*.jsx,**/*.ts,**/*.tsx
alwaysApply: false
---
# Next.js rules
- Use the App Router structure with `page.tsx` files in route directories.
- Client components must be explicitly marked with `'use client'` at the top of the file.
- Use kebab-case for directory names (e.g., `components/auth-form`) and PascalCase for component files.
- Prefer named exports over default exports, i.e. `export function Button() { /* ... */ }` instead of `export default function Button() { /* ... */ }`.
- Minimize `'use client'` directives:
- Keep most components as React Server Components (RSC)
- Only use client components when you need interactivity and wrap in `Suspense` with fallback UI
- Create small client component wrappers around interactive elements
- Avoid unnecessary `useState` and `useEffect` when possible:
- Use server components for data fetching
- Use React Server Actions for form handling
- Use URL search params for shareable state
- Use `nuqs` for URL search param state management
This rule provides guidance for Next.js development within Cursor, focusing on best practices for the App Router structure, component organization, and efficient state management techniques. It helps ensure your Next.js projects follow modern conventions and performance standards.
The Next.js rule in Cursor serves as a comprehensive guide for fullstack development using Next.js, particularly with the newer App Router architecture. It establishes conventions for:
These guidelines help maintain consistency across your Next.js projects while promoting patterns that enhance performance and developer experience.
The rule emphasizes using the App Router structure, which uses directory-based routing with page.tsx
files within route directories. This approach, introduced in Next.js 13, replaces the older Pages Router system with a more intuitive and flexible routing mechanism.
Several important component practices are recommended:
components/auth-form
)// Recommended
export function Button() {
/* ... */
}
// Not recommended
export default function Button() {
/* ... */
}
The rule provides guidance on the effective use of React Server Components (RSC) and Client Components:
'use client'
when they require client-side interactivitySuspense
with appropriate fallback UIThe rule recommends optimizing state management by:
nuqs
library for URL search parameter state managementThe Next.js rule (nextjs.mdc
) is configured to automatically activate when you're working with JavaScript or TypeScript files. The glob pattern **/*.js,**/*.jsx,**/*.ts,**/*.tsx
ensures that the AI gets this context whenever you're editing relevant files.
When active, this rule guides Cursor's AI to make recommendations and generate code that follows Next.js best practices. For example:
You can also manually invoke this rule by typing @nextjs
in Cursor's Cmd-K interface or chat if you need explicit Next.js guidance for a specific task.
One of the key performance optimizations in modern Next.js is minimizing the use of client components. Consider this pattern for interactive elements:
// ButtonWrapper.tsx - Small client component
'use client'
import { useState } from 'react'
export function ButtonWrapper({ children }) {
const [count, setCount] = useState(0)
return (
<button onClick={() => setCount(count + 1)}>
{children} {count}
</button>
)
}
// Page.tsx - Server component that uses the client component
import { ButtonWrapper } from './ButtonWrapper'
export default function Page() {
// This component remains a server component
return (
<div>
<h1>My Page</h1>
<ButtonWrapper>Click me</ButtonWrapper>
</div>
)
}
For state that needs to be shareable or persisted between page loads, the rule recommends using URL search parameters with the nuqs
library:
'use client'
import { useQueryState } from 'nuqs'
export function FilterComponent() {
// This will manage the 'category' state in the URL
const [category, setCategory] = useQueryState('category')
return (
<select
value={category || ''}
onChange={(e) => setCategory(e.target.value)}
>
<option value="">All</option>
<option value="electronics">Electronics</option>
<option value="clothing">Clothing</option>
</select>
)
}
Following these practices will help you build performant, maintainable Next.js applications that leverage the full capabilities of the framework.