Nextjs Best Practices

davila7/claude-code-templates/cli-tool/components/skills/development/nextjs-best-practices

作者 davila78da17d671b6f无许可证32K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库今天更新

Next.js App Router principles. Server Components, data fetching, routing patterns.

AI 生成的概览

关于 Next.js App Router 最佳实践的参考指南,涵盖服务端与客户端组件、数据获取、路由、缓存和性能。

功能
提供一套结构化的 Next.js App Router 原则,分为服务端与客户端组件、数据获取模式、路由约定、API 路由处理器、性能、元数据、缓存、服务端操作和反模式等章节。以决策树、对比表格和示例项目结构的形式呈现参考资料。产出的是指导建议,而非代码或文件。
适用场景
适用于规划或审查 Next.js App Router 代码库,并需要组件边界、数据获取、路由或缓存方面约定时。也适合在向服务端优先模式重构时作为检查清单使用。
运行要求
无需脚本或工具,仅为说明性指令。根据其声明的允许工具,代理需要读取、写入、编辑、glob 和 grep 工具。

Next.js Best Practices

Principles for Next.js App Router development.


1. Server vs Client Components

Decision Tree

Does it need...?│├── useState, useEffect, event handlers│   └── Client Component ('use client')│├── Direct data fetching, no interactivity│   └── Server Component (default)│└── Both?     └── Split: Server parent + Client child

By Default

TypeUse
ServerData fetching, layout, static content
ClientForms, buttons, interactive UI

2. Data Fetching Patterns

Fetch Strategy

PatternUse
DefaultStatic (cached at build)
RevalidateISR (time-based refresh)
No-storeDynamic (every request)

Data Flow

SourcePattern
DatabaseServer Component fetch
APIfetch with caching
User inputClient state + server action

3. Routing Principles

File Conventions

FilePurpose
page.tsxRoute UI
layout.tsxShared layout
loading.tsxLoading state
error.tsxError boundary
not-found.tsx404 page

Route Organization

PatternUse
Route groups (name)Organize without URL
Parallel routes @slotMultiple same-level pages
Intercepting (.)Modal overlays

4. API Routes

Route Handlers

MethodUse
GETRead data
POSTCreate data
PUT/PATCHUpdate data
DELETERemove data

Best Practices

  • Validate input with Zod
  • Return proper status codes
  • Handle errors gracefully
  • Use Edge runtime when possible

5. Performance Principles

Image Optimization

  • Use next/image component
  • Set priority for above-fold
  • Provide blur placeholder
  • Use responsive sizes

Bundle Optimization

  • Dynamic imports for heavy components
  • Route-based code splitting (automatic)
  • Analyze with bundle analyzer

6. Metadata

Static vs Dynamic

TypeUse
Static exportFixed metadata
generateMetadataDynamic per-route

Essential Tags

  • title (50-60 chars)
  • description (150-160 chars)
  • Open Graph images
  • Canonical URL

7. Caching Strategy

Cache Layers

LayerControl
Requestfetch options
Datarevalidate/tags
Full routeroute config

Revalidation

MethodUse
Time-basedrevalidate: 60
On-demandrevalidatePath/Tag
No cacheno-store

8. Server Actions

Use Cases

  • Form submissions
  • Data mutations
  • Revalidation triggers

Best Practices

  • Mark with 'use server'
  • Validate all inputs
  • Return typed responses
  • Handle errors

9. Anti-Patterns

❌ Don't✅ Do
'use client' everywhereServer by default
Fetch in client componentsFetch in server
Skip loading statesUse loading.tsx
Ignore error boundariesUse error.tsx
Large client bundlesDynamic imports

10. Project Structure

app/├── (marketing)/     # Route group│   └── page.tsx├── (dashboard)/│   ├── layout.tsx   # Dashboard layout│   └── page.tsx├── api/│   └── [resource]/│       └── route.ts└── components/    └── ui/

Remember: Server Components are the default for a reason. Start there, add client only when needed.

来源与署名

来源:davila7/claude-code-templates位于cli-tool/components/skills/development/nextjs-best-practices提交8da17d6

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架