next-best-practices

作者: sanity-io

Next.js 最佳实践 - 文件约定、RSC边界、数据模式、异步API、元数据、错误处理、路由处理器、图片/字体优化…

npx skills add https://github.com/sanity-io/next-sanity --skill next-best-practices

Next.js Best Practices

Apply these rules when writing or reviewing Next.js code.

File Conventions

See file-conventions.md for:

  • Project structure and special files
  • Route segments (dynamic, catch-all, groups)
  • Parallel and intercepting routes
  • Middleware rename in v16 (middleware → proxy)

RSC Boundaries

Detect invalid React Server Component patterns.

See rsc-boundaries.md for:

  • Async client component detection (invalid)
  • Non-serializable props detection
  • Server Action exceptions

Async Patterns

Next.js 15+ async API changes.

See async-patterns.md for:

  • Async params and searchParams
  • Async cookies() and headers()
  • Migration codemod

Runtime Selection

See runtime-selection.md for:

  • Default to Node.js runtime
  • When Edge runtime is appropriate

Directives

See directives.md for:

  • 'use client', 'use server' (React)
  • 'use cache' (Next.js)

Functions

See functions.md for:

  • Navigation hooks: useRouter, usePathname, useSearchParams, useParams
  • Server functions: cookies, headers, draftMode, after
  • Generate functions: generateStaticParams, generateMetadata

Error Handling

See error-handling.md for:

  • error.tsx, global-error.tsx, not-found.tsx
  • redirect, permanentRedirect, notFound
  • forbidden, unauthorized (auth errors)
  • unstable_rethrow for catch blocks

Data Patterns

See data-patterns.md for:

  • Server Components vs Server Actions vs Route Handlers
  • Avoiding data waterfalls (Promise.all, Suspense, preload)
  • Client component data fetching

Route Handlers

See route-handlers.md for:

  • route.ts basics
  • GET handler conflicts with page.tsx
  • Environment behavior (no React DOM)
  • When to use vs Server Actions

Metadata & OG Images

See metadata.md for:

  • Static and dynamic metadata
  • generateMetadata function
  • OG image generation with next/og
  • File-based metadata conventions

Image Optimization

See image.md for:

  • Always use next/image over <img>
  • Remote images configuration
  • Responsive sizes attribute
  • Blur placeholders
  • Priority loading for LCP

Font Optimization

See font.md for:

  • next/font setup
  • Google Fonts, local fonts
  • Tailwind CSS integration
  • Preloading subsets

Bundling

See bundling.md for:

  • Server-incompatible packages
  • CSS imports (not link tags)
  • Polyfills (already included)
  • ESM/CommonJS issues
  • Bundle analysis

Scripts

See scripts.md for:

  • next/script vs native script tags
  • Inline scripts need id
  • Loading strategies
  • Google Analytics with @next/third-parties

Hydration Errors

See hydration-error.md for:

  • Common causes (browser APIs, dates, invalid HTML)
  • Debugging with error overlay
  • Fixes for each cause

Suspense Boundaries

See suspense-boundaries.md for:

  • CSR bailout with useSearchParams and usePathname
  • Which hooks require Suspense boundaries

Parallel & Intercepting Routes

See parallel-routes.md for:

  • Modal patterns with @slot and (.) interceptors
  • default.tsx for fallbacks
  • Closing modals correctly with router.back()

Self-Hosting

See self-hosting.md for:

  • output: 'standalone' for Docker
  • Cache handlers for multi-instance ISR
  • What works vs needs extra setup

Debug Tricks

See debug-tricks.md for:

  • MCP endpoint for AI-assisted debugging
  • Rebuild specific routes with --debug-build-paths

来自 sanity-io 的更多技能

sanity-migration
sanity-io
规划、实施并审查从其他CMS和内容系统迁移至Sanity的过程。适用于从AEM、Adobe Experience Manager、Contentful、Strapi、Webflow、WordPress、Payload、Drupal、Markdown/MDX/frontmatter文件、WXR/XML导出、CMS API、数据库转储、静态HTML进行迁移或平台重构,或设计数据提取、转换、Portable Text转换、资产迁移、重定向、验证及切换工作流时使用。
officialdevelopmentdatabase
create-agent-with-sanity-context
sanity-io
通过Agent Context构建对Sanity内容的结构化访问的AI代理。用于设置由Sanity驱动的聊天机器人、将AI助手连接到Sanity…
official
dial-your-context
sanity-io
交互式会话,用于为Sanity Agent Context MCP创建指令字段内容。当用户提到调整代理上下文、改进……时,使用此技能。
official
optimize-agent-prompt
sanity-io
通过引导式对话调整你的Sanity Agent Context代理。将探索数据转化为可用于生产的指令,并构建系统提示词……
official
shape-your-agent
sanity-io
交互式会话,用于为基于Sanity Agent Context MCP的AI代理编写系统提示。当用户希望定义代理个性时使用此技能…
official
content-experimentation-best-practices
sanity-io
结构化指导,用于设计、执行和分析内容实验,以提升转化率和参与度。涵盖假设框架、指标选择、样本量计算以及A/B和多变量实验中的统计显著性检验。包含关于p值、置信区间、功效分析和贝叶斯方法的详细资源,用于解读结果。提供CMS集成模式,用于在字段级别管理变体并连接外部...
official
content-modeling-best-practices
sanity-io
结构化内容建模指南,涵盖模式设计、可复用性及多渠道交付。核心原则包括:将内容视为数据而非页面、维护单一事实来源、面向未来渠道设计、优化编辑工作流。提供引用与嵌入对象的选择框架、关注点分离及内容复用模式。包含扁平化、层级化及分面分类法的分类学指导。适用于...
official
portable-text-conversion
sanity-io
将HTML和Markdown内容转换为适用于Sanity的Portable Text块。在从旧版CMS迁移内容、将HTML或Markdown导入Sanity时使用。
official