技能 nextjs-best-practices
📦

nextjs-best-practices

安全

掌握 Next.js App Router 最佳实践

自信地构建生产级 Next.js 应用程序。了解服务器组件、数据获取策略和可扩展的路由模式。

支援: Claude Codex Code(CC)
🥉 74 青銅
1

下載技能 ZIP

2

在 Claude 中上傳

前往 設定 → 功能 → 技能 → 上傳技能

3

開啟並開始使用

測試它

正在使用「nextjs-best-practices」。 Should I use useState in a Server Component?

預期結果:

No. Server Components cannot use useState, useEffect, or event handlers. These hooks require browser JavaScript. If you need state, mark the component with 'use client' or extract the interactive parts into a separate Client Component.

正在使用「nextjs-best-practices」。 How do I cache API responses in Next.js?

預期結果:

Use the fetch options: fetch(url, { next: { revalidate: 60 } }) caches for 60 seconds. For static content, omit revalidate for build-time caching. Use 'no-store' to disable caching entirely.

正在使用「nextjs-best-practices」。 What is the purpose of loading.tsx?

預期結果:

loading.tsx provides automatic loading states during navigation and data fetching. It renders while the new segment loads, improving perceived performance. Place it alongside page.tsx in any route segment.

安全審計

安全
v2 • 2/24/2026

This skill is documentation-only content describing Next.js App Router best practices. Static analysis flagged 18 patterns (external_commands, weak cryptographic algorithms), but all are FALSE POSITIVES. The SKILL.md file contains only educational markdown content with code examples - no executable code, no shell commands, no cryptographic operations. Safe for publication.

1
已掃描檔案
209
分析行數
0
發現項
2
審計總數
未發現安全問題
審計者: claude 查看審計歷史 →

品質評分

38
架構
100
可維護性
87
內容
50
社群
100
安全
91
規範符合性

你能建構什麼

从 Pages 迁移到 App Router

使用正确的服务器组件模式将现有 Next.js 项目过渡到 App Router 架构。

构建新的 Next.js 应用程序

从第一天起就以正确的文件夹结构、缓存策略和组件架构启动新项目。

代码审查和优化

审计现有代码库中的反模式,如不必要的客户端组件或缺失的加载状态。

試試這些提示

组件类型决策
I have a component that displays user profile data and has a follow button. Should this be a Server Component or Client Component? Explain the reasoning.
数据获取策略
What is the recommended fetch pattern for displaying product listings that should update every 5 minutes? Show the implementation.
路由组织
How do I organize a dashboard with multiple sections (analytics, settings, users) that share a common layout but should not affect the URL structure?
完整功能实现
Create a comment form using Server Actions. Include input validation with Zod, optimistic UI updates, and error handling. Show both the Server Component and Client Component code.

最佳實務

  • 默认从服务器组件开始,仅在需要交互性时添加客户端组件
  • 使用带括号的路由组来组织路由,同时不影响 URL 路径
  • 为每个主要路由段实现 loading.tsx 和 error.tsx 以处理待处理状态和错误状态

避免

  • 为每个组件添加 'use client' - 这违背了服务器组件的目的并增加打包大小
  • 在客户端组件中而非服务器组件中获取数据 - 导致瀑布式请求和加载变慢
  • 跳过正确的错误边界 - 未处理的错误会导致整个应用程序崩溃,而不是显示后备 UI

常見問題

何时应该使用服务器组件 vs 客户端组件?
默认使用服务器组件进行数据获取、布局和静态内容。仅在需要 useState、useEffect、事件处理程序或浏览器 API 时使用客户端组件。拆分大型组件以保持客户端部分最小化。
App Router 中的缓存如何工作?
Next.js 默认缓存 fetch 请求。使用 revalidate 进行基于时间的重新验证 (ISR),使用 revalidatePath 或 revalidateTag 进行按需更新,使用 no-store 完全禁用缓存。每个路由段也可以有自己的缓存配置。
什么是服务器操作以及何时应该使用它们?
服务器操作是带有 'use server' 标记的异步函数,在服务器上运行。将其用于表单提交、数据变异和触发重新验证。始终正确验证输入并处理错误。
如何在 App Router 中处理身份验证?
在渲染受保护内容之前在服务器组件中检查身份验证。使用中间件进行路由级保护。对于客户端组件,从服务器组件获取身份验证状态或使用客户端身份验证提供程序。
layout.tsx 和 template.tsx 有什么区别?
layout.tsx 在导航之间保持不变并维护状态。template.tsx 每次导航时创建新实例,重新挂载子组件。将布局用于导航等持久 UI,在需要重置行为时使用模板。
我可以使用 App Router 的 API 路由吗?
可以。在 app 目录中创建带有 GET、POST、PUT、PATCH、DELETE 处理程序的 route.ts 文件。将路由处理程序用于 API 端点。考虑为简单端点使用 Edge 运行时以获得更好性能。

開發者詳情

檔案結構

📄 SKILL.md