mcpField
N
nextjs-app-router-patterns

nextjs-app-router-patterns

github.com/wshobson開発41.9k32.9k 回インストール2 週間前に更新
リポジトリ

nextjs-app-router-patterns について

App Router を使った Next.js 14 以降の開発や移行に取り組む開発者向けのパターンガイドです。レンダリング、ルーティング、データ処理を扱います。

主な機能

  1. 01Server Components と Client Components
  2. 02静的・動的・ストリーミングレンダリング
  3. 03並列ルートとインターセプトルート
  4. 04データ取得・キャッシュ・Server Actions

活用シーン

  1. 01Pages Router からの移行
  2. 02Server Components とストリーミングページの実装
  3. 03Server Actions を使うフルスタック機能の構築

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/wshobson/agents --skill nextjs-app-router-patterns

インストール後、Claude Code で/nextjs-app-router-patternsを入力して読み込まれたか確認します。

同梱ファイルreferences/details.md

詳細

Next.js App Router パターン

Next.js 14+ の App Router アーキテクチャ、Server Components、およびモダンなフルスタック React 開発のための包括的なパターンです。

このスキルを使う場面

  • App Router を使って新しい Next.js アプリケーションを構築する
  • Pages Router から App Router へ移行する
  • Server Components とストリーミングを実装する
  • 並列ルートとインターセプトルートを設定する
  • データ取得とキャッシュを最適化する
  • Server Actions を使ってフルスタック機能を構築する

コアコンセプト

1. レンダリングモード
モード場所使用する場面
Server Componentsサーバーのみデータ取得、重い計算、シークレット
Client Componentsブラウザインタラクティブ性、フック、ブラウザ API
Staticビルド時ほとんど変化しないコンテンツ
Dynamicリクエスト時パーソナライズまたはリアルタイムデータ
Streamingプログレッシブ大きなページ、遅いデータソース
2. ファイル規約
app/
├── layout.tsx       # Shared UI wrapper
├── page.tsx         # Route UI
├── loading.tsx      # Loading UI (Suspense)
├── error.tsx        # Error boundary
├── not-found.tsx    # 404 UI
├── route.ts         # API endpoint
├── template.tsx     # Re-mounted layout
├── default.tsx      # Parallel route fallback
└── opengraph-image.tsx  # OG image generation

クイックスタート

// app/layout.tsx
import { Inter } from 'next/font/google'
import { Providers } from './providers'

const inter = Inter({ subsets: ['latin'] })

export const metadata = {
  title: { default: 'My App', template: '%s | My App' },
  description: 'Built with Next.js App Router',
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body className={inter.className}>
        <Providers>{children}</Providers>
      </body>
    </html>
  )
}

// app/page.tsx - Server Component by default
async function getProducts() {
  const res = await fetch('https://api.example.com/products', {
    next: { revalidate: 3600 }, // ISR: revalidate every hour
  })
  return res.json()
}

export default async function HomePage() {
  const products = await getProducts()

  return (
    <main>
      <h1>Products</h1>
      <ProductGrid products={products} />
    </main>
  )
}

詳細なパターンと実例

詳細なパターンのドキュメントは references/details.md にあります。上記のナビゲーション階層では不十分な場合は、そのファイルを読んでください。

ベストプラクティス

推奨事項
  • Server Components から始める - 必要な場合にのみ 'use client' を追加する
  • データ取得をコロケーションする - データは使われる場所で取得する
  • Suspense バウンダリを使う - 遅いデータに対してストリーミングを有効にする
  • 並列ルートを活用する - 独立したローディング状態
  • Server Actions を使う - プログレッシブエンハンスメントを伴うミューテーションに
非推奨事項
  • シリアライズ可能なデータを渡さない - Server → Client 境界の制約
  • Server Components でフックを使わない - useState、useEffect はなし
  • Client Components でフェッチしない - Server Components または React Query を使う
  • レイアウトを過度にネストしない - 各レイアウトはコンポーネントツリーに追加される
  • ローディング状態を無視しない - 常に loading.tsx または Suspense を提供する

コメント