Next.js Best Practices
Apply these rules when writing or reviewing Next.js code.
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 and usePathname
- Which hooks require Suspense boundaries
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
Cache Components (Next.js 16+)
See the next-cache skill for:
cacheComponents: true in next.config.ts (replaces experimental.ppr)
'use cache' directive for async data components
cacheLife() presets: 'seconds', 'minutes', 'hours', 'days', 'weeks', 'max'
cacheTag() + revalidateTag() for on-demand invalidation
- Three content types: Static, Cached (
use cache), Dynamic (Suspense)
Source: suleman-se/martnex — distributed by TomeVault.
1---2name: next-patterns3description: Next.js best practices - file conventions, RSC boundaries, data patterns, async APIs, metadata, error handling, route handlers, image/font optimization, bundling Use when this capability is needed.4---56# Next.js Best Practices78Apply these rules when writing or reviewing Next.js code.910## File Conventions1112See [file-conventions.md](./reference/file-conventions.md) for:13- Project structure and special files14- Route segments (dynamic, catch-all, groups)15- Parallel and intercepting routes16- Middleware rename in v16 (middleware → proxy)1718## RSC Boundaries1920Detect invalid React Server Component patterns.2122See [rsc-boundaries.md](./reference/rsc-boundaries.md) for:23- Async client component detection (invalid)24- Non-serializable props detection25- Server Action exceptions2627## Async Patterns2829Next.js 15+ async API changes.3031See [async-patterns.md](./reference/async-patterns.md) for:32- Async `params` and `searchParams`33- Async `cookies()` and `headers()`34- Migration codemod3536## Runtime Selection3738See [runtime-selection.md](./reference/runtime-selection.md) for:39- Default to Node.js runtime40- When Edge runtime is appropriate4142## Directives4344See [directives.md](./reference/directives.md) for:45- `'use client'`, `'use server'` (React)46- `'use cache'` (Next.js)4748## Functions4950See [functions.md](./reference/functions.md) for:51- Navigation hooks: `useRouter`, `usePathname`, `useSearchParams`, `useParams`52- Server functions: `cookies`, `headers`, `draftMode`, `after`53- Generate functions: `generateStaticParams`, `generateMetadata`5455## Error Handling5657See [error-handling.md](./reference/error-handling.md) for:58- `error.tsx`, `global-error.tsx`, `not-found.tsx`59- `redirect`, `permanentRedirect`, `notFound`60- `forbidden`, `unauthorized` (auth errors)61- `unstable_rethrow` for catch blocks6263## Data Patterns6465See [data-patterns.md](./reference/data-patterns.md) for:66- Server Components vs Server Actions vs Route Handlers67- Avoiding data waterfalls (`Promise.all`, Suspense, preload)68- Client component data fetching6970## Route Handlers7172See [route-handlers.md](./reference/route-handlers.md) for:73- `route.ts` basics74- GET handler conflicts with `page.tsx`75- Environment behavior (no React DOM)76- When to use vs Server Actions7778## Metadata & OG Images7980See [metadata.md](./reference/metadata.md) for:81- Static and dynamic metadata82- `generateMetadata` function83- OG image generation with `next/og`84- File-based metadata conventions8586## Image Optimization8788See [image.md](./reference/image.md) for:89- Always use `next/image` over `<img>`90- Remote images configuration91- Responsive `sizes` attribute92- Blur placeholders93- Priority loading for LCP9495## Font Optimization9697See [font.md](./reference/font.md) for:98- `next/font` setup99- Google Fonts, local fonts100- Tailwind CSS integration101- Preloading subsets102103## Bundling104105See [bundling.md](./reference/bundling.md) for:106- Server-incompatible packages107- CSS imports (not link tags)108- Polyfills (already included)109- ESM/CommonJS issues110- Bundle analysis111112## Scripts113114See [scripts.md](./reference/scripts.md) for:115- `next/script` vs native script tags116- Inline scripts need `id`117- Loading strategies118- Google Analytics with `@next/third-parties`119120## Hydration Errors121122See [hydration-error.md](./reference/hydration-error.md) for:123- Common causes (browser APIs, dates, invalid HTML)124- Debugging with error overlay125- Fixes for each cause126127## Suspense Boundaries128129See [suspense-boundaries.md](./reference/suspense-boundaries.md) for:130- CSR bailout with `useSearchParams` and `usePathname`131- Which hooks require Suspense boundaries132133## Parallel & Intercepting Routes134135See [parallel-routes.md](./reference/parallel-routes.md) for:136- Modal patterns with `@slot` and `(.)` interceptors137- `default.tsx` for fallbacks138- Closing modals correctly with `router.back()`139140## Self-Hosting141142See [self-hosting.md](./reference/self-hosting.md) for:143- `output: 'standalone'` for Docker144- Cache handlers for multi-instance ISR145- What works vs needs extra setup146147## Debug Tricks148149See [debug-tricks.md](./reference/debug-tricks.md) for:150- MCP endpoint for AI-assisted debugging151- Rebuild specific routes with `--debug-build-paths`152153## Cache Components (Next.js 16+)154155See the [next-cache skill](../next-cache/SKILL.md) for:156- `cacheComponents: true` in `next.config.ts` (replaces `experimental.ppr`)157- `'use cache'` directive for async data components158- `cacheLife()` presets: `'seconds'`, `'minutes'`, `'hours'`, `'days'`, `'weeks'`, `'max'`159- `cacheTag()` + `revalidateTag()` for on-demand invalidation160- Three content types: Static, Cached (`use cache`), Dynamic (Suspense)161162---163> Source: [suleman-se/martnex](https://github.com/suleman-se/martnex) — distributed by [TomeVault](https://tomevault.io).164<!-- tomevault:4.0:skill_md:2026-06-15 -->