next-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.
在编写或评审 Next.js 代码时,请遵循以下规则。
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 的重命名(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 15+ async API changes.
See async-patterns.md for:
- Async and
paramssearchParams - Async and
cookies()headers() - Migration codemod
Next.js 15+ 版本的异步 API 变更。
请查看 async-patterns.md 了解:
- 异步 与
paramssearchParams - 异步 与
cookies()headers() - 迁移代码转换工具(codemod)
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、Server Actions 与 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 标签)
- 已内置的 Polyfills
- 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
水化错误
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