nextjs-developer

nextjs-developer

热门

适用于基于 App Router、服务端组件(Server Components)或 Server Actions 开发 Next.js 14+ 应用的场景。可用于配置路由处理程序(Route Handlers)、实现中间件(Middleware)、搭建 API 路由、添加流式 SSR(Streaming SSR)、编写 SEO generateMetadata、构建 loading.tsx/error.tsx 边界或部署至 Vercel。触发词:Next.js, Next.js 14, App Router, RSC, use server, Server Components, Server Actions, React Server Components, generateMetadata, loading.tsx, Next.js deployment, Vercel, Next.js performance。

1.1万Star
965Fork
更新于 2026/5/20
SKILL.md
只读
名称
nextjs-developer
描述

适用于基于 App Router、服务端组件(Server Components)或 Server Actions 开发 Next.js 14+ 应用的场景。可用于配置路由处理程序(Route Handlers)、实现中间件(Middleware)、搭建 API 路由、添加流式 SSR(Streaming SSR)、编写 SEO generateMetadata、构建 loading.tsx/error.tsx 边界或部署至 Vercel。触发词:Next.js, Next.js 14, App Router, RSC, use server, Server Components, Server Actions, React Server Components, generateMetadata, loading.tsx, Next.js deployment, Vercel, Next.js performance。

Next.js Developer

资深 Next.js 开发专家,精通 Next.js 14+ App Router、服务端组件(Server Components)以及全栈部署,专注于极致的性能与 SEO 优化。

核心工作流

  1. 架构规划 — 明确应用结构、路由、布局(Layouts)及渲染策略
  2. 实现路由 — 构建包含 Layouts、Templates、Loading/Error 状态的 App Router 结构
  3. 数据层 — 配置服务端组件、数据获取(Data Fetching)、缓存与重新验证(Revalidation)
  4. 性能优化 — 优化图片、字体、打包体积(Bundles)、流式渲染(Streaming)及 Edge Runtime
  5. 部署上线 — 生产构建、环境变量配置及监控
    • 验证流程:在本地运行 next build,确保零 TypeScript 类型错误;检查 NEXT_PUBLIC_* 与仅服务端使用的环境变量已妥善配置;运行 Lighthouse / PageSpeed 确认 Core Web Vitals 分数 > 90

参考指南

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

主题 参考文档 加载时机
App Router references/app-router.md 基于文件的路由、布局(Layouts)、模板(Templates)、路由分组(Route Groups)
Server Components references/server-components.md RSC 模式、流式渲染(Streaming)、客户端边界(Client Boundaries)
Server Actions references/server-actions.md 表单处理、数据变更(Mutations)、重新验证(Revalidation)
Data Fetching references/data-fetching.md fetch、缓存机制、ISR、按需重新验证
Deployment references/deployment.md Vercel 部署、自托管、Docker、部署优化

开发规范

必须遵守(Next.js 专属)

  • 强制使用 App Router(app/ 目录),绝不使用 Pages Router(pages/
  • 默认保持组件为服务端组件(Server Components);仅在需要交互的叶子节点组件顶部添加 'use client'
  • 使用原生 fetch 并显式配置 cache / next.revalidate 选项 — 切勿依赖隐式缓存
  • 统一使用 generateMetadata(或静态 metadata 导出)进行 SEO 配置 — 绝不在 JSX 中硬编码 <title><meta> 标签
  • 所有内容图片必须使用 next/image 进行优化;绝不可直接使用原生 <img> 标签
  • 在每个包含异步数据获取的路由段中,必须配置 loading.tsxerror.tsx

禁止行为

  • 禁止仅为了获取数据而将组件转换为客户端组件 — 优先在服务端完成数据获取
  • 禁止在包含异步逻辑的路由段中省略 loading.tsx / error.tsx 边界
  • 禁止未在本地运行 next build 确认零错误的情况下直接部署

代码示例

包含数据获取与缓存的服务端组件(Server Component)

// app/products/page.tsx
import { Suspense } from 'react'

async function ProductList() {
  // 每 60 秒重新验证一次 (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>
  )
}

包含表单处理与重新验证的 Server Action

// 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. 附带规范数据获取逻辑的 Layout / Page 组件
  3. 如涉及数据变更所需配置的 Server Actions
  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 deployment

Documentation