Next.js Best Practices
Apply these rules when writing or reviewing Next.js code.
Cache Components patterns: When the project has cacheComponents: true in next.config.ts,
use the separate cache-components skill for 'use cache', cacheLife(), cacheTag(),
updateTag(), and revalidateTag() guidance.
File Conventions
See file-conventions.md for:
- Project structure and special files
- Route segments (dynamic, catch-all, groups)
- Parallel and intercepting routes
- Middleware rename in v16 (middleware → proxy)
RSC Boundaries
Detect invalid React Server Component patterns.
See rsc-boundaries.md for:
- Async client component detection (invalid)
- Non-serializable props detection
- Server Action exceptions
Async Patterns
Next.js 15+ async API changes.
See async-patterns.md for:
- Async
params and searchParams
- Async
cookies() and headers()
- Migration codemod
Runtime Selection
See runtime-selection.md for:
- Default to Node.js runtime
- When Edge runtime is appropriate
Directives
See directives.md for:
'use client', 'use server' (React)
'use cache' (Next.js)
Functions
See functions.md for:
- Navigation hooks:
useRouter, usePathname, useSearchParams, useParams
- Server functions:
cookies, headers, draftMode, after
- Generate functions:
generateStaticParams, generateMetadata
Error Handling
See error-handling.md for:
error.tsx, global-error.tsx, not-found.tsx
redirect, permanentRedirect, notFound
forbidden, unauthorized (auth errors)
unstable_rethrow for catch blocks
Data Patterns
See data-patterns.md for:
- Server Components vs Server Actions vs Route Handlers
- Avoiding data waterfalls (
Promise.all, Suspense, preload)
- Client component data fetching
Route Handlers
See route-handlers.md for:
route.ts basics
- GET handler conflicts with
page.tsx
- Environment behavior (no React DOM)
- When to use vs Server Actions
Metadata & OG Images
See metadata.md for:
- Static and dynamic metadata
generateMetadata function
- OG image generation with
next/og
- File-based metadata conventions
Image Optimization
See image.md for:
- Always use
next/image over <img>
- Remote images configuration
- Responsive
sizes attribute
- Blur placeholders
- Priority loading for LCP
Font Optimization
See font.md for:
next/font setup
- Google Fonts, local fonts
- Tailwind CSS integration
- Preloading subsets
Bundling
See bundling.md for:
- Server-incompatible packages
- CSS imports (not link tags)
- Polyfills (already included)
- ESM/CommonJS issues
- Bundle analysis
Scripts
See scripts.md for:
next/script vs native script tags
- Inline scripts need
id
- Loading strategies
- Google Analytics with
@next/third-parties
Hydration Errors
See hydration-error.md for:
- Common causes (browser APIs, dates, invalid HTML)
- Debugging with error overlay
- Fixes for each cause
Suspense Boundaries
See suspense-boundaries.md for:
- CSR bailout with
useSearchParams (only this hook requires a boundary)
- Forcing dynamic rendering with
connection() instead
Parallel & Intercepting Routes
See parallel-routes.md for:
- Modal patterns with
@slot and (.) interceptors
default.tsx for fallbacks
- Closing modals correctly with
router.back()
Self-Hosting
See self-hosting.md for:
output: 'standalone' for Docker
- Cache handlers for multi-instance ISR
- What works vs needs extra setup
Debug Tricks
See debug-tricks.md for:
- MCP endpoint for AI-assisted debugging
- Rebuild specific routes with
--debug-build-paths
1---2name: next-best-practices3description: Next.js App Router best practices covering file conventions, RSC boundaries, async APIs, data patterns, hydration errors, metadata, route handlers, image/font optimization, and bundling. Use when writing or reviewing Next.js code to prevent hydration errors, RSC violations, data waterfalls, and configuration mistakes.4---56# Next.js Best Practices78Apply these rules when writing or reviewing Next.js code.910> **Cache Components patterns**: When the project has `cacheComponents: true` in `next.config.ts`,11> use the separate `cache-components` skill for `'use cache'`, `cacheLife()`, `cacheTag()`,12> `updateTag()`, and `revalidateTag()` guidance.1314## File Conventions1516See [file-conventions.md](./file-conventions.md) for:17- Project structure and special files18- Route segments (dynamic, catch-all, groups)19- Parallel and intercepting routes20- Middleware rename in v16 (middleware → proxy)2122## RSC Boundaries2324Detect invalid React Server Component patterns.2526See [rsc-boundaries.md](./rsc-boundaries.md) for:27- Async client component detection (invalid)28- Non-serializable props detection29- Server Action exceptions3031## Async Patterns3233Next.js 15+ async API changes.3435See [async-patterns.md](./async-patterns.md) for:36- Async `params` and `searchParams`37- Async `cookies()` and `headers()`38- Migration codemod3940## Runtime Selection4142See [runtime-selection.md](./runtime-selection.md) for:43- Default to Node.js runtime44- When Edge runtime is appropriate4546## Directives4748See [directives.md](./directives.md) for:49- `'use client'`, `'use server'` (React)50- `'use cache'` (Next.js)5152## Functions5354See [functions.md](./functions.md) for:55- Navigation hooks: `useRouter`, `usePathname`, `useSearchParams`, `useParams`56- Server functions: `cookies`, `headers`, `draftMode`, `after`57- Generate functions: `generateStaticParams`, `generateMetadata`5859## Error Handling6061See [error-handling.md](./error-handling.md) for:62- `error.tsx`, `global-error.tsx`, `not-found.tsx`63- `redirect`, `permanentRedirect`, `notFound`64- `forbidden`, `unauthorized` (auth errors)65- `unstable_rethrow` for catch blocks6667## Data Patterns6869See [data-patterns.md](./data-patterns.md) for:70- Server Components vs Server Actions vs Route Handlers71- Avoiding data waterfalls (`Promise.all`, Suspense, preload)72- Client component data fetching7374## Route Handlers7576See [route-handlers.md](./route-handlers.md) for:77- `route.ts` basics78- GET handler conflicts with `page.tsx`79- Environment behavior (no React DOM)80- When to use vs Server Actions8182## Metadata & OG Images8384See [metadata.md](./metadata.md) for:85- Static and dynamic metadata86- `generateMetadata` function87- OG image generation with `next/og`88- File-based metadata conventions8990## Image Optimization9192See [image.md](./image.md) for:93- Always use `next/image` over `<img>`94- Remote images configuration95- Responsive `sizes` attribute96- Blur placeholders97- Priority loading for LCP9899## Font Optimization100101See [font.md](./font.md) for:102- `next/font` setup103- Google Fonts, local fonts104- Tailwind CSS integration105- Preloading subsets106107## Bundling108109See [bundling.md](./bundling.md) for:110- Server-incompatible packages111- CSS imports (not link tags)112- Polyfills (already included)113- ESM/CommonJS issues114- Bundle analysis115116## Scripts117118See [scripts.md](./scripts.md) for:119- `next/script` vs native script tags120- Inline scripts need `id`121- Loading strategies122- Google Analytics with `@next/third-parties`123124## Hydration Errors125126See [hydration-error.md](./hydration-error.md) for:127- Common causes (browser APIs, dates, invalid HTML)128- Debugging with error overlay129- Fixes for each cause130131## Suspense Boundaries132133See [suspense-boundaries.md](./suspense-boundaries.md) for:134- CSR bailout with `useSearchParams` (only this hook requires a boundary)135- Forcing dynamic rendering with `connection()` instead136137## Parallel & Intercepting Routes138139See [parallel-routes.md](./parallel-routes.md) for:140- Modal patterns with `@slot` and `(.)` interceptors141- `default.tsx` for fallbacks142- Closing modals correctly with `router.back()`143144## Self-Hosting145146See [self-hosting.md](./self-hosting.md) for:147- `output: 'standalone'` for Docker148- Cache handlers for multi-instance ISR149- What works vs needs extra setup150151## Debug Tricks152153See [debug-tricks.md](./debug-tricks.md) for:154- MCP endpoint for AI-assisted debugging155- Rebuild specific routes with `--debug-build-paths`156