Communitygithub.com

sakilanwar12/nextjs-typescript-skills

用於建構 Next.js App Router 頁面、佈局、載入/錯誤狀態、路由群組、平行或攔截路由。涵蓋檔案慣例、RSC 邊界及導航。

nextjs-typescript-skills 是什麼?

這項技能專為使用 Next.js 14+ 搭配 `app/` 目錄與 TypeScript 的開發者設計,提供建構 App Router 所需的核心知識與最佳實踐。它能協助 AI 代理正確生成頁面、佈局、載入狀態、錯誤處理及各種進階路由模式(如平行路由與攔截路由)。技能內含詳細的檔案慣例說明、明確的開發規則(如預設伺服器元件、僅在必要時使用 `'use client'`),以及實用的程式碼範例與檢查清單。支援平台包括 Claude Code、Cursor 與 Codex 等常見 AI 編碼工具,確保生成的 Next.js 程式碼符合官方建議與新版本(Next 15)的非同步 `params` 特性。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/sakilanwar12/nextjs-typescript-skills/tree/HEAD/skills/nextjs-app-router

在你喜歡的 AI 中提問

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

說明文件

Next.js App Router

適用於 Next.js 14+ 搭配 app/ 目錄與 TypeScript。

檔案慣例

  • app/layout.tsx — 必要的根佈局。必須包含 <html> 與 <body>。
  • app/page.tsx — 路由入口。預設為 React Server Component (RSC)。
  • app/loading.tsx — 該分段(segment)的串流後備(streaming fallback)。
  • app/error.tsx — 必須是客戶端元件(Client Component,加上 'use client'),接收 { error, reset }。
  • app/not-found.tsx — 404 頁面。
  • app/route.ts — Route Handler (API)。匯出 GET、POST 等。
  • 動態路由:app/blog/[slug]/page.tsx 取得 { params: { slug: string } }。
  • 路由群組:app/(marketing)/about/page.tsx — 不影響 URL。
  • 平行路由:app/@modal/page.tsx + layout({ modal })。攔截路由:app/(.)photo/[id]/page.tsx。

規則

  1. 預設使用伺服器元件(Server Components)。僅在需要使用 hooks、事件或瀏覽器 API 時才加上 'use client'。保持客戶端邊界最小,將它們推到葉節點。
  2. 在 Next 15 中,params 與 searchParams 為非同步:await params。
  3. 若伺服器元件使用了僅限伺服器的功能,切勿將其直接匯入客戶端元件 — 改以 children/props 傳遞。
  4. 使用 next/link 進行導航,next/navigation(useRouter、usePathname)僅用於客戶端元件。在伺服器元件中使用 next/navigation 的 redirect() / notFound()。
  5. 協同佈置:每個分段放置 page.tsx、loading.tsx、error.tsx。共用 UI 放在 components/,除非是路由頁面,否則不要放在 app/。

範例

// app/blog/[slug]/page.tsx — Next 15 非同步 params
type Props = { params: Promise<{ slug: string }> };

export default async function BlogPost({ params }: Props) {
  const { slug } = await params;
  const post = await getPost(slug);
  if (!post) notFound();
  return <article>{post.title}</article>;
}

export async function generateStaticParams() {
  const slugs = await getAllSlugs();
  return slugs.map((slug) => ({ slug }));
}
// app/error.tsx — 必須是客戶端元件
'use client';
export default function Error({ error, reset }: { error: Error; reset: () => void }) {
  return (
    <div>
      <h2>發生錯誤</h2>
      <button onClick={() => reset()}>重試</button>
    </div>
  );
}
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

檢查清單

  • 檔名與分段放置正確嗎?
  • 客戶端指令('use client')僅在必要處使用?
  • 非同步分段有提供 loading.tsx / error.tsx?
  • 需要 SEO 的地方有使用 generateStaticParams + generateMetadata?
  • 沒有從 next/router(pages-router 的匯入)使用 useRouter?

Individual skills in this repo

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

相關技能