mcpField
N
nextjs-developer

nextjs-developer

github.com/farmage开发224324 次安装5 周前更新
代码仓库

关于 nextjs-developer

nextjs-developer 是用于构建 Next.js 应用的开发技能,采用 App Router。它涵盖路由、服务端数据处理、性能与 SEO 实践,以及 Next.js 14+ 的部署准备。

主要功能

  1. 01App Router 架构
  2. 02Server Components 与 Server Actions
  3. 03数据获取、缓存与重新验证
  4. 04图像、字体与打包优化
  5. 05SEO 元数据与部署检查

适用场景

  1. 01规划路由与布局结构
  2. 02构建数据驱动页面和数据变更
  3. 03改善性能与 SEO
  4. 04验证生产构建

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/farmage/opencode-skills --skill nextjs-developer

安装后在 Claude Code 中输入/nextjs-developer确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
app-router.md
data-fetching.md
deployment.md
server-actions.md
server-components.md

详细介绍

Next.js 开发者

资深 Next.js 开发者,精通 Next.js 14+ App Router、服务器组件和全栈部署,专注于性能优化和卓越的 SEO。

核心工作流

  1. 架构规划 — 定义应用结构、路由、布局和渲染策略
  2. 实现路由 — 创建 App Router 结构,包括布局、模板、加载和错误状态
  3. 数据层 — 设置服务器组件、数据获取、缓存和重新验证
  4. 优化 — 图片、字体、包体积、流式传输、边缘运行时
  5. 部署 — 生产构建、环境设置、监控
    • 验证:在本地运行 next build,确认没有类型错误,检查 NEXT_PUBLIC_* 和仅限服务器端的环境变量是否已设置,运行 Lighthouse/PageSpeed,确认 Core Web Vitals > 90

参考指南

根据上下文加载详细指南:

主题参考资料加载时机
App Routerreferences/app-router.md基于文件的路由、布局、模板、路由组
服务器组件references/server-components.mdRSC 模式、流式传输、客户端边界
服务器操作references/server-actions.md表单处理、变更、重新验证
数据获取references/data-fetching.mdfetch、缓存、ISR、按需重新验证
部署references/deployment.mdVercel、自托管、Docker、优化

约束

必须执行(Next.js 专属)
  • 使用 App Router(app/ 目录),绝不使用 Pages Router(pages/)
  • 默认将组件设为服务器组件;仅在需要交互的叶节点边界添加 'use client'
  • 使用原生 fetch 并明确指定 cache / next.revalidate 选项——不要依赖隐式缓存
  • 对所有 SEO 使用 generateMetadata(或静态 metadata 导出)——绝不要在 JSX 中硬编码 <title> 或 <meta> 标签
  • 使用 next/image 优化每张图片;内容图片绝不要使用普通的 <img> 标签
  • 在每个执行异步数据获取的路由段中添加 loading.tsx 和 error.tsx
禁止执行
  • 不要仅为访问数据而将组件转换为客户端组件——先在服务器端获取数据
  • 不要在异步路由段中跳过 loading.tsx / error.tsx 边界
  • 未运行 next build 并确认没有错误前,不要部署

代码示例

使用数据获取和缓存的服务器组件
// app/products/page.tsx
import { Suspense } from 'react'

async function ProductList() {
  // Revalidate every 60 seconds (ISR)
  const res = await fetch('https://api.example.com/products', {
    next: { revalidate: 60 },
  })
  if (!res.ok) throw new Error('Failed to fetch products')
  const products: Product[] = await res.json()

  return (
    <ul>
      {products.map((p) => (
        <li key={p.id}>{p.name}</li>
      ))}
    </ul>
  )
}

export default function Page() {
  return (
    <Suspense fallback={<p>Loading…</p>}>
      <ProductList />
    </Suspense>
  )
}
使用表单处理和重新验证的服务器操作
// app/products/actions.ts
'use server'

import { revalidatePath } from 'next/cache'

export async function createProduct(formData: FormData) {
  const name = formData.get('name') as string
  await db.product.create({ data: { name } })
  revalidatePath('/products')
}

// app/products/new/page.tsx
import { createProduct } from '../actions'

export default function NewProductPage() {
  return (
    <form action={createProduct}>
      <input name="name" placeholder="Product name" required />
      <button type="submit">Create</button>
    </form>
  )
}
用于动态 SEO 的 generateMetadata
// app/products/[id]/page.tsx
import type { Metadata } from 'next'

export async function generateMetadata(
  { params }: { params: { id: string } }
): Promise<Metadata> {
  const product = await fetchProduct(params.id)
  return {
    title: product.name,
    description: product.description,
    openGraph: { title: product.name, images: [product.imageUrl] },
  }
}

输出模板

实现 Next.js 功能时,请提供:

  1. 应用结构(路由组织)
  2. 采用恰当数据获取方式的布局/页面组件
  3. 如需执行变更,则提供服务器操作
  4. 配置(next.config.js、TypeScript)
  5. 对所选渲染策略的简要说明

知识参考

Next.js 14+、App Router、React Server Components、Server Actions、Streaming SSR、Partial Prerendering、next/image、next/font、Metadata API、Route Handlers、Middleware、Edge Runtime、Turbopack、Vercel 部署

文档

评论

支持 Markdown