nextjs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNext.js Community Next.js 16 App Router Best Practices
Next.js 社区 Next.js 16 App Router 最佳实践
Comprehensive performance optimization guide for Next.js 16 App Router applications, maintained by the Next.js Community. Contains 40 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
由 Next.js 社区维护的 Next.js 16 App Router 应用性能优化综合指南。包含 8 个类别共 40 条规则,按影响优先级排序,可指导自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Next.js 16 App Router code
- Configuring caching strategies with 'use cache' directive
- Implementing server components and data fetching
- Setting up routing with parallel and intercepting routes
- Creating server actions for form handling and mutations
以下场景可参考本指南:
- 编写新的 Next.js 16 App Router 代码
- 使用 'use cache' 指令配置缓存策略
- 实现服务端组件(Server Components)和数据获取逻辑
- 设置并行路由与拦截路由
- 创建用于表单处理和数据变更的 Server Actions
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Build & Bundle Optimization | CRITICAL | |
| 2 | Caching Strategy | CRITICAL | |
| 3 | Server Components & Data Fetching | HIGH | |
| 4 | Routing & Navigation | HIGH | |
| 5 | Server Actions & Mutations | MEDIUM-HIGH | |
| 6 | Streaming & Loading States | MEDIUM | |
| 7 | Metadata & SEO | MEDIUM | |
| 8 | Client Components | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 构建与打包优化 | 关键(CRITICAL) | |
| 2 | 缓存策略 | 关键(CRITICAL) | |
| 3 | 服务端组件与数据获取 | 高(HIGH) | |
| 4 | 路由与导航 | 高(HIGH) | |
| 5 | Server Actions 与数据变更 | 中高(MEDIUM-HIGH) | |
| 6 | 流式处理与加载状态 | 中(MEDIUM) | |
| 7 | 元数据与 SEO | 中(MEDIUM) | |
| 8 | 客户端组件 | 中低(LOW-MEDIUM) | |
Quick Reference
快速参考
1. Build & Bundle Optimization (CRITICAL)
1. 构建与打包优化(关键)
- - Configure optimizePackageImports for Icon Libraries
build-optimize-package-imports - - Use Dynamic Imports for Heavy Components
build-dynamic-imports - - Avoid Barrel File Imports in App Router
build-barrel-files - - Enable Turbopack File System Caching
build-turbopack-config - - Configure Server External Packages for Node Dependencies
build-external-packages
- - 为图标库配置 optimizePackageImports
build-optimize-package-imports - - 对大型组件使用动态导入
build-dynamic-imports - - 在 App Router 中避免桶文件导入
build-barrel-files - - 启用 Turbopack 文件系统缓存
build-turbopack-config - - 为 Node 依赖配置服务端外部包
build-external-packages
2. Caching Strategy (CRITICAL)
2. 缓存策略(关键)
- - Use the 'use cache' Directive for Explicit Caching
cache-use-cache-directive - - Use revalidateTag with cacheLife Profiles
cache-revalidate-tag - - Configure Fetch Cache Options Correctly
cache-fetch-options - - Use revalidatePath for Route-Level Cache Invalidation
cache-revalidate-path - - Use React cache() for Request Deduplication
cache-react-cache - - Configure Route Segment Caching with Exports
cache-segment-config
- - 使用 'use cache' 指令进行显式缓存
cache-use-cache-directive - - 结合 cacheLife 配置文件使用 revalidateTag
cache-revalidate-tag - - 正确配置 Fetch 缓存选项
cache-fetch-options - - 使用 revalidatePath 进行路由级缓存失效
cache-revalidate-path - - 使用 React cache() 实现请求去重
cache-react-cache - - 通过导出配置路由段缓存
cache-segment-config
3. Server Components & Data Fetching (HIGH)
3. 服务端组件与数据获取(高)
- - Fetch Data in Parallel in Server Components
server-parallel-fetching - - Stream Server Components for Progressive Loading
server-component-streaming - - Colocate Data Fetching with Components
server-data-colocation - - Use Preload Pattern for Critical Data
server-preload-pattern - - Avoid Client-Side Data Fetching for Initial Data
server-avoid-client-fetching - - Handle Server Component Errors Gracefully
server-error-handling
- - 在服务端组件中并行获取数据
server-parallel-fetching - - 流式传输服务端组件以实现渐进式加载
server-component-streaming - - 将数据获取逻辑与组件就近放置
server-data-colocation - - 对关键数据使用预加载模式
server-preload-pattern - - 初始数据避免使用客户端数据获取
server-avoid-client-fetching - - 优雅处理服务端组件错误
server-error-handling
4. Routing & Navigation (HIGH)
4. 路由与导航(高)
- - Use Parallel Routes for Independent Content
route-parallel-routes - - Use Intercepting Routes for Modal Patterns
route-intercepting-routes - - Configure Link Prefetching Appropriately
route-prefetching - - Use proxy.ts for Network Boundary Logic
route-proxy-ts - - Use notFound() for Missing Resources
route-not-found
- - 使用并行路由实现独立内容展示
route-parallel-routes - - 使用拦截路由实现模态框模式
route-intercepting-routes - - 合理配置 Link 预加载
route-prefetching - - 使用 proxy.ts 处理网络边界逻辑
route-proxy-ts - - 使用 notFound() 处理资源缺失场景
route-not-found
5. Server Actions & Mutations (MEDIUM-HIGH)
5. Server Actions 与数据变更(中高)
- - Use Server Actions for Form Submissions
action-server-action-forms - - Show Pending States with useFormStatus
action-pending-states - - Handle Server Action Errors Gracefully
action-error-handling - - Use Optimistic Updates for Instant Feedback
action-optimistic-updates - - Revalidate Cache After Mutations
action-revalidation
- - 使用 Server Actions 处理表单提交
action-server-action-forms - - 结合 useFormStatus 展示等待状态
action-pending-states - - 优雅处理 Server Actions 错误
action-error-handling - - 使用乐观更新实现即时反馈
action-optimistic-updates - - 数据变更后重新验证缓存
action-revalidation
6. Streaming & Loading States (MEDIUM)
6. 流式处理与加载状态(中)
- - Place Suspense Boundaries Strategically
stream-suspense-boundaries - - Use loading.tsx for Route-Level Loading States
stream-loading-tsx - - Use error.tsx for Route-Level Error Boundaries
stream-error-tsx - - Match Skeleton Dimensions to Actual Content
stream-skeleton-matching - - Nest Suspense for Progressive Disclosure
stream-nested-suspense
- - 合理设置 Suspense 边界
stream-suspense-boundaries - - 使用 loading.tsx 实现路由级加载状态
stream-loading-tsx - - 使用 error.tsx 实现路由级错误边界
stream-error-tsx - - 保持骨架屏尺寸与实际内容一致
stream-skeleton-matching - - 嵌套使用 Suspense 实现渐进式内容展示
stream-nested-suspense
7. Metadata & SEO (MEDIUM)
7. 元数据与 SEO(中)
- - Use generateMetadata for Dynamic Metadata
meta-generate-metadata - - Generate Sitemaps Dynamically
meta-sitemap - - Configure Robots for Crawl Control
meta-robots - - Generate Dynamic OpenGraph Images
meta-opengraph-images
- - 使用 generateMetadata 生成动态元数据
meta-generate-metadata - - 动态生成站点地图
meta-sitemap - - 配置 Robots 协议控制爬虫行为
meta-robots - - 动态生成 OpenGraph 图片
meta-opengraph-images
8. Client Components (LOW-MEDIUM)
8. 客户端组件(中低)
- - Minimize 'use client' Boundary Scope
client-use-client-boundary - - Pass Server Components as Children to Client Components
client-children-pattern - - Avoid Hydration Mismatches
client-hydration-mismatch - - Load Third-Party Scripts Efficiently
client-third-party-scripts
- - 最小化 'use client' 边界范围
client-use-client-boundary - - 将服务端组件作为子组件传递给客户端组件
client-children-pattern - - 避免 hydration 不匹配问题
client-hydration-mismatch - - 高效加载第三方脚本
client-third-party-scripts
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
- build-dynamic-imports - Example rule file
- cache-use-cache-directive - Example rule file
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构与影响级别说明
- 规则模板 - 添加新规则的模板
- build-dynamic-imports - 规则示例文件
- cache-use-cache-directive - 规则示例文件
Related Skills
相关技能
- For React 19 fundamentals, see skill
react-19 - For data fetching patterns, see skill
tanstack-query - For client-side forms, see skill
react-hook-form
- React 19 基础知识请参考 技能
react-19 - 数据获取模式请参考 技能
tanstack-query - 客户端表单请参考 技能
react-hook-form
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md包含所有规则详细内容的完整指南:
AGENTS.md