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 | 构建与打包优化 | 关键 | |
| 2 | 缓存策略 | 关键 | |
| 3 | Server Components与数据获取 | 高 | |
| 4 | 路由与导航 | 高 | |
| 5 | Server Actions与数据变更 | 中高 | |
| 6 | 流式传输与加载状态 | 中 | |
| 7 | 元数据与SEO | 中 | |
| 8 | Client Components | 中低 | |
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 - - Configure Turbopack at the top level
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依赖配置Server External Packages
build-external-packages
2. Caching Strategy (CRITICAL)
2. 缓存策略(关键)
- - Use 'use cache' with Cache Components
cache-use-cache-directive - - Use revalidateTag with cacheLife Profiles
cache-revalidate-tag - - Configure Fetch Cache Options Explicitly
cache-fetch-options - - Use revalidatePath for Route-Level Cache Invalidation
cache-revalidate-path - - Use React cache() for Request Deduplication
cache-react-cache - - Match route configuration to the active cache model
cache-segment-config
- - 结合Cache Components使用'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. Server Components与数据获取(高)
- - 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 Components中并行获取数据
server-parallel-fetching - - 流式传输Server Components以实现渐进式加载
server-component-streaming - - 将数据获取逻辑与组件放在同一位置
server-data-colocation - - 对关键数据使用预加载模式
server-preload-pattern - - 初始数据避免使用客户端数据获取
server-avoid-client-fetching - - 优雅处理Server Components错误
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. Client Components(中低)
- - 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 - - 将Server Components作为子组件传递给Client Components
client-children-pattern - - 避免 hydration 不匹配问题
client-hydration-mismatch - - 高效加载第三方脚本
client-third-party-scripts
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- 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 - For data fetching patterns, see skill
tanstack-query - For client-side forms, see skill
react-hook-form
- React 19基础内容请查看技能文档
react - 数据获取模式请查看技能文档
tanstack-query - 客户端表单请查看技能文档
react-hook-form
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md包含所有规则详细说明的完整指南:
AGENTS.md