nextjs-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNext.js Best Practices
Next.js 最佳实践
Apply these rules when writing or reviewing Next.js code.
Note: Next.js 16 renamedtomiddleware.ts. Verifyproxy.tssupport in your version;proxy.tsremains the stable API.middleware.ts
编写或评审 Next.js 代码时请遵循以下规则。
注意: Next.js 16 已将重命名为middleware.ts。请确认你的版本是否支持proxy.ts;proxy.ts仍是稳定 API。middleware.ts
Table of Contents
目录
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)
详见 file-conventions.md:
- 项目结构与特殊文件
- 路由分段(动态、捕获所有、分组)
- 并行与拦截路由
- v16 中的中间件重命名(middleware → proxy)
RSC Boundaries
RSC 边界
Detect invalid React Server Component patterns.
See rsc-boundaries.md for:
- Async client component detection (invalid)
- Non-serializable props detection
- Server Action exceptions
检测无效的 React Server Component 模式。
详见 rsc-boundaries.md:
- 异步客户端组件检测(无效)
- 不可序列化 props 检测
- Server Action 异常
Async Patterns
异步模式
Next.js 16.1.6+ async API changes.
See async-patterns.md for:
- Async and
paramssearchParams - Async and
cookies()headers() - Migration codemod
Next.js 16.1.6+ 的异步 API 变更。
详见 async-patterns.md:
- 异步 与
paramssearchParams - 异步 与
cookies()headers() - 迁移代码转换工具
Runtime Selection
运行时选择
See runtime-selection.md for:
- Default to Node.js runtime
- When Edge runtime is appropriate
详见 runtime-selection.md:
- 默认使用 Node.js 运行时
- 适合使用 Edge 运行时的场景
Directives
指令
See directives.md for:
- ,
'use client'(React)'use server' - (Next.js)
'use cache'
详见 directives.md:
- 、
'use client'(React)'use server' - (Next.js)
'use cache'
Functions
函数
See functions.md for:
- Navigation hooks: ,
useRouter,usePathname,useSearchParamsuseParams - Server functions: ,
cookies,headers,draftModeafter - Generate functions: ,
generateStaticParamsgenerateMetadata
详见 functions.md:
- 导航钩子:、
useRouter、usePathname、useSearchParamsuseParams - 服务器函数:、
cookies、headers、draftModeafter - 生成函数:、
generateStaticParamsgenerateMetadata
Error Handling
错误处理
See error-handling.md for:
- ,
error.tsx,global-error.tsxnot-found.tsx - ,
redirect,permanentRedirectnotFound - ,
forbidden(auth errors)unauthorized - for catch blocks
unstable_rethrow
详见 error-handling.md:
- 、
error.tsx、global-error.tsxnot-found.tsx - 、
redirect、permanentRedirectnotFound - 、
forbidden(认证错误)unauthorized - 用于捕获块的
unstable_rethrow
Data Patterns
数据模式
See data-patterns.md for:
- Server Components vs Server Actions vs Route Handlers
- Avoiding data waterfalls (, Suspense, preload)
Promise.all - Client component data fetching
详见 data-patterns.md:
- Server Components vs Server Actions vs Route Handlers
- 避免数据瀑布(、Suspense、预加载)
Promise.all - 客户端组件数据获取
Route Handlers
路由处理器
See route-handlers.md for:
- basics
route.ts - GET handler conflicts with
page.tsx - Environment behavior (no React DOM)
- When to use vs Server Actions
详见 route-handlers.md:
- 基础
route.ts - GET 处理器与 的冲突
page.tsx - 环境行为(无 React DOM)
- 何时使用 vs Server Actions
Metadata & OG Images
元数据与 OG 图片
See metadata.md for:
- Static and dynamic metadata
- function
generateMetadata - OG image generation with
next/og - File-based metadata conventions
详见 metadata.md:
- 静态与动态元数据
- 函数
generateMetadata - 使用 生成 OG 图片
next/og - 基于文件的元数据约定
Image Optimization
图片优化
See image.md for:
- Always use over
next/image<img> - Remote images configuration
- Responsive attribute
sizes - Blur placeholders
- Priority loading for LCP
详见 image.md:
- 始终使用 而非
next/image<img> - 远程图片配置
- 响应式 属性
sizes - 模糊占位符
- LCP 优先加载
Font Optimization
字体优化
See font.md for:
- setup
next/font - Google Fonts, local fonts
- Tailwind CSS integration
- Preloading subsets
详见 font.md:
- 配置
next/font - Google Fonts、本地字体
- Tailwind CSS 集成
- 预加载子集
Bundling
打包
See bundling.md for:
- Server-incompatible packages
- CSS imports (not link tags)
- Polyfills (already included)
- ESM/CommonJS issues
- Bundle analysis
详见 bundling.md:
- 服务器不兼容的包
- CSS 导入(而非 link 标签)
- Polyfill(已内置)
- ESM/CommonJS 问题
- 包分析
Scripts
脚本
See scripts.md for:
- vs native script tags
next/script - Inline scripts need
id - Loading strategies
- Google Analytics with
@next/third-parties
详见 scripts.md:
- vs 原生 script 标签
next/script - 内联脚本需要
id - 加载策略
- 使用 集成 Google Analytics
@next/third-parties
Hydration Errors
Hydration 错误
See hydration-error.md for:
- Common causes (browser APIs, dates, invalid HTML)
- Debugging with error overlay
- Fixes for each cause
详见 hydration-error.md:
- 常见原因(浏览器 API、日期、无效 HTML)
- 使用错误覆盖层调试
- 各原因对应的修复方案
Suspense Boundaries
Suspense 边界
See suspense-boundaries.md for:
- CSR bailout with and
useSearchParamsusePathname - Which hooks require Suspense boundaries
详见 suspense-boundaries.md:
- 使用 和
useSearchParams时的 CSR 回退usePathname - 哪些钩子需要 Suspense 边界
Parallel & Intercepting Routes
并行与拦截路由
See parallel-routes.md for:
- Modal patterns with and
@slotinterceptors(.) - for fallbacks
default.tsx - Closing modals correctly with
router.back()
详见 parallel-routes.md:
- 使用 和
@slot拦截器实现模态框模式(.) - 回退使用
default.tsx - 使用 正确关闭模态框
router.back()
Self-Hosting
自托管
See self-hosting.md for:
- for Docker
output: 'standalone' - Cache handlers for multi-instance ISR
- What works vs needs extra setup
详见 self-hosting.md:
- Docker 部署使用
output: 'standalone' - 多实例 ISR 的缓存处理器
- 原生支持的功能 vs 需要额外配置的功能
Debug Tricks
调试技巧
See debug-tricks.md for:
- MCP endpoint for AI-assisted debugging
- Rebuild specific routes with
--debug-build-paths
详见 debug-tricks.md:
- 用于 AI 辅助调试的 MCP 端点
- 使用 重新构建特定路由
--debug-build-paths