跳到正文
Skillpedia
网站与界面(前端)48k · #5301

nextjs-best-practices

它能做什么

这份指南教你按照最佳实践使用 Next.js。Next.js 是一个用于构建网站和网页应用的框架。指南会讲解如何在服务器和浏览器之间拆分代码、如何获取数据,以及如何组织页面。

适用场景

  • ✓开始一个新的 Next.js 项目时。
  • ✓想通过重构现有项目来提升性能时。
  • ✓想了解服务端组件和客户端组件时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/nextjs-best-practices/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/nextjs-best-practices/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我该如何组织 Next.js 项目中的页面?”

每周 5 个新技能,1 分钟读完。

工作原理

Next.js 会把工作分配给服务器(负责生成页面)和浏览器(用户与页面交互的地方)。它借助文件约定和特定模式来优化速度和用户体验。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

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

Type Use
Server Data fetching, layout, static content
Client Forms, buttons, interactive UI

2. Data Fetching Patterns

Fetch Strategy

Pattern Use
Default Static (cached at build)
Revalidate ISR (time-based refresh)
No-store Dynamic (every request)

Data Flow

Source Pattern
Database Server Component fetch
API fetch with caching
User input Client state + server action

3. Routing Principles

File Conventions

File Purpose
page.tsx Route UI
layout.tsx Shared layout
loading.tsx Loading state
error.tsx Error boundary
not-found.tsx 404 page

Route Organization

Pattern Use
Route groups (name) Organize without URL
Parallel routes @slot Multiple same-level pages
Intercepting (.) Modal overlays

4. API Routes

Route Handlers

Method Use
GET Read data
POST Create data
PUT/PATCH Update data
DELETE Remove 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

Type Use
Static export Fixed metadata
generateMetadata Dynamic per-route

Essential Tags

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

7. Caching Strategy

Cache Layers

Layer Control
Request fetch options
Data revalidate/tags
Full route route config

Revalidation

Method Use
Time-based revalidate: 60
On-demand revalidatePath/Tag
No cache no-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' everywhere Server by default
Fetch in client components Fetch in server
Skip loading states Use loading.tsx
Ignore error boundaries Use error.tsx
Large client bundles Dynamic 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.

When to Use

This skill is applicable to execute the workflow or actions described in the overview.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
常见问题

服务端组件和客户端组件是什么?

服务端组件在服务器上运行,适合获取数据。客户端组件在浏览器中运行,用于实现交互效果,比如按钮和表单。

Next.js 如何获取数据?

你可以根据需要选择静态获取(在构建时)、定期更新,或者每次请求时获取。

什么是 Server Actions?

它们是在服务器上运行的函数,用于执行提交表单、更新数据等操作,既安全又高效。

详情

作者的原始描述

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

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#5301
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能