Communitygithub.com

Next.js 最佳實踐

Next.js App Router 原則:伺服器元件、資料擷取、路由模式。

Next.js 最佳實踐 是什麼?

此技能旨在提供一套完整的 Next.js App Router 開發最佳實踐指南。它詳細解釋了伺服器元件 (Server Components) 與客戶端元件 (Client Components) 的決策邏輯,幫助開發者從預設的伺服器元件開始,只有在需要互動性時才使用客戶端元件。技能涵蓋了資料擷取策略(靜態、ISR、動態)、路由組織(路由群組、平行路由、攔截路由)、API 路由處理、圖片與打包效能優化、中繼資料設定、快取策略以及伺服器動作等核心主題。此外,還列出了常見的反模式與對應的正確做法,並提供了一個建議的專案結構。整體而言,這份指南適用於希望在 Claude Code、Cursor 或 Codex 等 AI 代理平台中快速撰寫符合 Next.js 官方建議的程式碼的開發者,能顯著減少錯誤,提升應用程式的效能與可維護性。

相容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/marnixGeerkens/rocketlauncher-claude-code/tree/HEAD/components/skills/development/nextjs-best-practices

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Next.js 最佳實務

Next.js App Router 開發原則。


1. 伺服器元件 vs 用戶端元件

決策樹

是否需要...?
│
├── useState、useEffect、事件處理器
│   └── 用戶端元件('use client')
│
├── 直接資料擷取,無互動性
│   └── 伺服器元件(預設)
│
└── 兩者都需要?
    └── 拆分:伺服器父元件 + 用戶端子元件

預設情況下

類型用途
伺服器資料擷取、版面配置、靜態內容
用戶端表單、按鈕、互動式 UI

2. 資料擷取模式

擷取策略

模式用途
預設靜態(建置時快取)
RevalidateISR(基於時間的重新驗證)
No-store動態(每次請求)

資料流

來源模式
資料庫伺服器元件擷取
API使用快取的 fetch
使用者輸入用戶端狀態 + 伺服器動作

3. 路由原則

檔案慣例

檔案用途
page.tsx路由 UI
layout.tsx共用版面配置
loading.tsx載入狀態
error.tsx錯誤邊界
not-found.tsx404 頁面

路由組織

模式用途
路由群組 (name)在不影響 URL 的情況下組織
平行路由 @slot同一層級的多個頁面
攔截路由 (.)模態覆蓋層

4. API 路由

路由處理器

方法用途
GET讀取資料
POST建立資料
PUT/PATCH更新資料
DELETE刪除資料

最佳實務

  • 使用 Zod 驗證輸入
  • 回傳正確的狀態碼
  • 優雅地處理錯誤
  • 盡可能使用 Edge Runtime

5. 效能原則

圖片最佳化

  • 使用 next/image 元件
  • 為折疊以上的圖片設定 priority
  • 提供模糊佔位符
  • 使用響應式尺寸

套件最佳化

  • 對重量級元件使用動態匯入 (Dynamic imports)
  • 基於路由的程式碼分割(自動)
  • 使用 Bundle Analyzer 進行分析

6. 中繼資料 (Metadata)

靜態 vs 動態

類型用途
靜態匯出固定的中繼資料
generateMetadata每個路由動態產生

必要標籤

  • title(50-60 字元)
  • description(150-160 字元)
  • Open Graph 圖片
  • 標準網址 (Canonical URL)

7. 快取策略

快取層

層級控制
請求fetch 選項
資料revalidate / tags
完整路由路由設定

重新驗證

方法用途
基於時間revalidate: 60
按需觸發revalidatePath / revalidateTag
無快取no-store

8. 伺服器動作 (Server Actions)

使用案例

  • 表單提交
  • 資料變更
  • 重新驗證觸發

最佳實務

  • 使用 'use server' 標記
  • 驗證所有輸入
  • 回傳型別化的響應
  • 處理錯誤

9. 反模式 (Anti-Patterns)

❌ 不要這樣做✅ 應該這樣做
到處使用 'use client'預設使用伺服器元件
在客戶端元件中擷取資料在伺服器中擷取資料
跳過載入狀態使用 loading.tsx
忽略錯誤邊界使用 error.tsx
大的客戶端套件使用動態匯入

10. 專案結構

app/
├── (marketing)/     # 路由群組
│   └── page.tsx
├── (dashboard)/
│   ├── layout.tsx   # Dashboard 版面配置
│   └── page.tsx
├── api/
│   └── [資源]/
│       └── route.ts
└── components/
    └── ui/

切記: 伺服器元件是預設選項是有原因的。從伺服器元件開始,只有在需要時才加入客戶端元件。

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

marnixGeerkens/rocketlauncher-claude-code

When the user wants to optimize, improve, or increase conversions on any marketing page — including homepage, landing pages, pricing pages, feature pages, or blog posts. Also use when the user says "CRO," "conversion rate optimization," "this page isn't converting," "improve conversions," or "why isn't this page working." For signup/registration flows, see signup-flow-cro. For post-signup activation, see onboarding-cro. For forms outside of signup, see form-cro. For popups/modals, see popup-cro.

marnixGeerkens/rocketlauncher-claude-code

Balance visual design with ATS compatibility for creative roles. Use when the user is in design, art, writing, marketing creative, or any visual/creative field and needs a resume that looks distinctive but still parses.

marnixGeerkens/rocketlauncher-claude-code

Transform resume bullets into detailed portfolio case studies with context, action, and outcome. Use when the user mentions portfolio, case study, project write-up, or expanding a resume bullet into a longer narrative.

marnixGeerkens/rocketlauncher-claude-code

Expert in building portfolios that actually land jobs and clients - not just showing work, but creating memorable experiences. Covers developer portfolios, designer portfolios, creative portfolios, and portfolios that convert visitors into opportunities. Use when: portfolio, personal website, showcase work, developer portfolio, designer portfolio.

marnixGeerkens/rocketlauncher-claude-code

Tailwind CSS v4 principles. CSS-first configuration, container queries, modern patterns, design token architecture.

marnixGeerkens/rocketlauncher-claude-code

Frontend development patterns for React, Next.js, state management, performance optimization, and UI best practices.

marnixGeerkens/rocketlauncher-claude-code

Frontend development guidelines for React/TypeScript applications. Modern patterns including Suspense, lazy loading, useSuspenseQuery, file organization with features directory, MUI v7 styling, TanStack Router, performance optimization, and TypeScript best practices. Use when creating components, pages, features, fetching data, styling, routing, or working with frontend code.

marnixGeerkens/rocketlauncher-claude-code

Expert integration of Supabase Auth with Next.js App Router Use when: supabase auth next, authentication next.js, login supabase, auth middleware, protected route.

marnixGeerkens/rocketlauncher-claude-code

Comprehensive frontend development skill for building modern, performant web applications using ReactJS, NextJS, TypeScript, Tailwind CSS. Includes component scaffolding, performance optimization, bundle analysis, and UI best practices. Use when developing frontend features, optimizing performance, implementing UI/UX designs, managing state, or reviewing frontend code.

marnixGeerkens/rocketlauncher-claude-code

Create API handoff documentation for frontend developers. Use when backend work is complete and needs to be documented for frontend integration, or user says 'create handoff', 'document API', 'frontend handoff', or 'API documentation'.

marnixGeerkens/rocketlauncher-claude-code

Document frontend data needs for backend developers. Use when frontend needs to communicate API requirements to backend, or user says 'backend requirements', 'what data do I need', 'API requirements', or is describing data needs for a UI.

marnixGeerkens/rocketlauncher-claude-code

Comprehensive Python library for astronomy and astrophysics. This skill should be used when working with astronomical data including celestial coordinates, physical units, FITS files, cosmological calculations, time systems, tables, world coordinate systems (WCS), and astronomical data analysis. Use when tasks involve coordinate transformations, unit conversions, FITS file manipulation, cosmological distance calculations, time scale conversions, or astronomical data processing.

marnixGeerkens/rocketlauncher-claude-code

Comprehensive patterns for Next.js 14+ App Router architecture, Server Components, and modern full-stack React development.

相關技能