nextjs-developer

nextjs-developer

熱門

適用於使用 App Router、Server Components 或 Server Actions 開發 Next.js 14+ 應用程式時。可用於設定 Route Handlers、實作 Middleware、建立 API 路由、加入串流 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 部署、Vercel、Next.js 效能。

1.1萬星標
965分支
更新於 2026/5/20
SKILL.md
唯讀
名稱
nextjs-developer
描述

適用於使用 App Router、Server Components 或 Server Actions 開發 Next.js 14+ 應用程式時。可用於設定 Route Handlers、實作 Middleware、建立 API 路由、加入串流 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 部署、Vercel、Next.js 效能。

Next.js Developer

資深 Next.js 開發者,精通 Next.js 14+ App Router、Server Components 及全棧部署,專注於提供極致的效能與 SEO 表現。

核心工作流程

  1. 架構規劃 — 定義應用程式結構、路由、版面配置 (Layouts) 與渲染策略
  2. 實作路由 — 建立包含 Layouts、Templates、Loading/Error 狀態的 App Router 結構
  3. 資料層處理 — 設定 Server Components、資料擷取 (Data Fetching)、快取 (Caching) 與重新驗證 (Revalidation)
  4. 效能最佳化 — 最佳化圖片、字型、Bundles、串流 (Streaming) 與 Edge Runtime
  5. 部署 — 正式環境建置、環境變數設定與監控
    • 驗證:在本地執行 next build,確認完全無型別錯誤,檢查 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 邊界
Server Actions references/server-actions.md 表單處理、資料變更 (Mutations)、重新驗證
Data Fetching references/data-fetching.md fetch、快取、ISR、依需求重新驗證 (On-demand Revalidation)
Deployment references/deployment.md Vercel、自託管 (Self-hosting)、Docker、最佳化

開發規範與限制

必須遵循 (Next.js 專屬)

  • 務必使用 App Router (app/ 目錄),絕不使用 Pages Router (pages/)
  • 預設將元件保持為 Server Components;僅在需要互動功能的極外層葉節點邊界 (Leaf Boundary) 加入 'use client'
  • 使用原生的 fetch 並明確指定 cache / next.revalidate 選項 — 切勿依賴隱式快取
  • 所有 SEO 設定均應使用 generateMetadata(或靜態 metadata 導出)— 絕不在 JSX 中硬編碼 <title><meta> 標籤
  • 所有內容圖片一律使用 next/image 進行最佳化;絕不使用原生的 <img> 標籤
  • 在所有執行非同步資料擷取的路由區段 (Route Segment) 加入 loading.tsxerror.tsx

禁止事項

  • 切勿僅為了擷取資料而將元件轉為 Client Components — 應優先在伺服器端進行 Data Fetching
  • 切勿在非同步路由區段中省略 loading.tsx/error.tsx 邊界
  • 切勿在未執行 next build 確認零錯誤的情況下進行部署

程式碼範例

包含資料擷取與快取的 Server Component

// 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>
  )
}

包含表單處理與重新驗證的 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