experimental.adapterPath
Next.js は、ビルドプロセスにフックするカスタムアダプターを作成できる実験的な API を提供します。これは、デプロイメントプラットフォームや、Next.js の設定を変更したりビルド出力を処理したりする必要があるカスタムビルド統合に役立ちます。
設定
アダプターを使用するには、`experimental.adapterPath` にアダプターモジュールへのパスを指定します。
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
adapterPath: require.resolve('./my-adapter.js'),
},
}
module.exports = nextConfigアダプターの作成
アダプターは、`NextAdapter` インターフェースを実装するオブジェクトをエクスポートするモジュールです。
export interface NextAdapter {
name: string
modifyConfig?: (
config: NextConfigComplete,
ctx: {
phase: PHASE_TYPE
}
) => Promise<NextConfigComplete> | NextConfigComplete
onBuildComplete?: (ctx: {
routes: {
headers: Array<ManifestHeaderRoute>
redirects: Array<ManifestRedirectRoute>
rewrites: {
beforeFiles: Array<ManifestRewriteRoute>
afterFiles: Array<ManifestRewriteRoute>
fallback: Array<ManifestRewriteRoute>
}
dynamicRoutes: ReadonlyArray<ManifestRoute>
}
outputs: AdapterOutputs
projectDir: string
repoRoot: string
distDir: string
config: NextConfigComplete
nextVersion: string
}) => Promise<void> | void
}基本的なアダプター構造
これは最小限のアダプターの例です。
/** @type {import('next').NextAdapter} */
const adapter = {
name: 'my-custom-adapter',
async modifyConfig(config, { phase }) {
// Modify the Next.js config based on the build phase
if (phase === 'phase-production-build') {
return {
...config,
// Add your modifications
}
}
return config
},
async onBuildComplete({
routes,
outputs,
projectDir,
repoRoot,
distDir,
config,
nextVersion,
}) {
// Process the build output
console.log('Build completed with', outputs.pages.length, 'pages')
// Access different output types
for (const page of outputs.pages) {
console.log('Page:', page.pathname, 'at', page.filePath)
}
for (const apiRoute of outputs.pagesApi) {
console.log('API Route:', apiRoute.pathname, 'at', apiRoute.filePath)
}
for (const appPage of outputs.appPages) {
console.log('App Page:', appPage.pathname, 'at', appPage.filePath)
}
for (const prerender of outputs.prerenders) {
console.log('Prerendered:', prerender.pathname)
}
},
}
module.exports = adapterAPIリファレンス
modifyConfig(config, context)
next.config をロードする CLI コマンドで、設定の変更を許可するために呼び出されます。
Parameters
config: 完全な Next.js 設定オブジェクトcontext.phase: 現在のビルドフェーズ (「フェーズ」を参照してください phases)
返り値: 変更された設定オブジェクト (非同期の場合もあります)
onBuildComplete(context)
ビルドプロセスが完了した後、ルーティングと出力に関する詳細情報とともに呼び出されます。
Parameters
routes: ルーティングマニフェスト(ヘッダー、リダイレクト、リライト、動的ルーティング)を含むオブジェクトroutes.headers: `source`、`sourceRegex`、`headers`、`has`、`missing`、およびオプションの `priority` フィールドを持つヘッダー ルート オブジェクトの配列routes.redirects: `source`、`sourceRegex`、`destination`、`statusCode`、`has`、`missing`、およびオプションの `priority` フィールドを持つリダイレクト ルート オブジェクトの配列routes.rewrites: `beforeFiles`、`afterFiles`、`fallback` の各配列を持つオブジェクト。それぞれ `source`、`sourceRegex`、`destination`、`has`、`missing` フィールドを持つリライト ルート オブジェクトを含みます。routes.dynamicRoutes: `source`、`sourceRegex`、`destination`、`has`、`missing` フィールドを持つ動的ルート オブジェクトの配列
outputs: タイプ別に整理されたすべてのビルド出力に関する詳細情報projectDir: Next.js プロジェクト ディレクトリへの絶対パスrepoRoot: 検出されたリポジトリ ルートへの絶対パスdistDir: ビルド出力ディレクトリへの絶対パスconfig: 最終的な Next.js 設定 (`modifyConfig` 適用後)nextVersion: 使用されている Next.js のバージョンbuildId: 現在のビルドの一意の識別子
出力タイプ
`outputs` オブジェクトには、さまざまな出力タイプの配列が含まれています。
ページ (`outputs.pages`)
`pages/` ディレクトリからの React ページ
{
type: 'PAGES'
id: string // Route identifier
filePath: string // Path to the built file
pathname: string // URL pathname
sourcePage: string // Original source file path in pages/ directory
runtime: 'nodejs' | 'edge'
assets: Record<string, string> // Traced dependencies (key: relative path from repo root, value: absolute path)
wasmAssets?: Record<string, string> // Bundled wasm files (key: name, value: absolute path)
config: {
maxDuration?: number
preferredRegion?: string | string[]
env?: Record<string, string> // Environment variables (edge runtime only)
}
}API Routes (`outputs.pagesApi`)
`pages/api/` からの API ルート
{
type: 'PAGES_API'
id: string
filePath: string
pathname: string
sourcePage: string // Original relative source file path
runtime: 'nodejs' | 'edge'
assets: Record<string, string>
wasmAssets?: Record<string, string>
config: {
maxDuration?: number
preferredRegion?: string | string[]
env?: Record<string, string>
}
}App Pages (`outputs.appPages`)
`page.{js,ts,jsx,tsx}` を持つ `app/` ディレクトリからの React ページ
{
type: 'APP_PAGE'
id: string
filePath: string
pathname: string // Includes .rsc suffix for RSC routes
sourcePage: string // Original relative source file path
runtime: 'nodejs' | 'edge'
assets: Record<string, string>
wasmAssets?: Record<string, string>
config: {
maxDuration?: number
preferredRegion?: string | string[]
env?: Record<string, string>
}
}App Routes (`outputs.appRoutes`)
`route.{js,ts,jsx,tsx}` を持つ `app/` からの API およびメタデータ ルート
{
type: 'APP_ROUTE'
id: string
filePath: string
pathname: string
sourcePage: string
runtime: 'nodejs' | 'edge'
assets: Record<string, string>
wasmAssets?: Record<string, string>
config: {
maxDuration?: number
preferredRegion?: string | string[]
env?: Record<string, string>
}
}Prerenders (`outputs.prerenders`)
ISR 対応ルートと静的プリレンダー
{
type: 'PRERENDER'
id: string
pathname: string
parentOutputId: string // ID of the source page/route
groupId: number // Revalidation group identifier (prerenders with same groupId revalidate together)
pprChain?: {
headers: Record<string, string> // PPR chain headers (e.g., 'x-nextjs-resume': '1')
}
parentFallbackMode?: 'blocking' | false | null // Fallback mode from getStaticPaths
fallback?: {
filePath: string
initialStatus?: number
initialHeaders?: Record<string, string | string[]>
initialExpiration?: number
initialRevalidate?: number
postponedState?: string // PPR postponed state
}
config: {
allowQuery?: string[] // Allowed query parameters
allowHeader?: string[] // Allowed headers for ISR
bypassFor?: RouteHas[] // Cache bypass conditions
renderingMode?: RenderingMode
bypassToken?: string
}
}Static Files (`outputs.staticFiles`)
静的アセットと自動的に静的最適化されたページ
{
type: 'STATIC_FILE'
id: string
filePath: string
pathname: string
}Middleware (`outputs.middleware`)
ミドルウェア関数 (存在する場合)
{
type: 'MIDDLEWARE'
id: string
filePath: string
pathname: string // Always '/_middleware'
sourcePage: string // Always 'middleware'
runtime: 'nodejs' | 'edge'
assets: Record<string, string>
wasmAssets?: Record<string, string>
config: {
maxDuration?: number
preferredRegion?: string | string[]
env?: Record<string, string>
matchers?: Array<{
source: string
sourceRegex: string
has: RouteHas[] | undefined
missing: RouteHas[] | undefined
}>
}
}ルーティング情報
`onBuildComplete` の `routes` オブジェクトは、デプロイメントの準備ができた処理済みパターンを含む、完全なルーティング情報を提供します。
ヘッダー
各ヘッダー ルートには以下が含まれます。
source: 元のルート パターン (例: `/about`)sourceRegex: リクエストを照合するためのコンパイル済み正規表現headers: 適用するヘッダーのキーと値のペアhas: 満たされる必要があるオプションの条件missing: 満たされない必要があるオプションの条件priority: 内部ルーティングのためのオプションのフラグ
リダイレクト
各リダイレクト ルートには以下が含まれます。
source: 元のルート パターンsourceRegex: 照合のためのコンパイル済み正規表現destination: ターゲット URL (キャプチャ グループを含む場合があります)statusCode: HTTP ステータス コード (301, 302, 307, 308)has: オプションの正の条件missing: オプションの負の条件priority: 内部ルーティングのためのオプションのフラグ
リライト
リライトは 3 つのフェーズに分類されます。
beforeFiles: ファイルシステム (ページおよびパブリック ファイルを含む) の前にチェックされます。afterFiles: ページ/パブリック ファイルの後、動的ルートの前にチェックされます。fallback: 他のすべてのルートの後にチェックされます。
各リライトには、`source`、`sourceRegex`、`destination`、`has`、および `missing` が含まれます。
動的ルーティング
動的ルート セグメント (例: `[slug]`、`[...path]`) から生成されます。それぞれ以下が含まれます。
source: ルート パターンsourceRegex: 名前付きキャプチャ グループを持つコンパイル済み正規表現destination: パラメータ置換を備えた内部宛先has: オプションの正の条件missing: オプションの負の条件
ユースケース
アダプターの一般的なユースケースには以下が含まれます。
- デプロイメント プラットフォーム統合: 特定のホスティング プラットフォームのビルド出力を自動的に構成する
- アセット処理: ビルド出力を変換または最適化する
- 監視統合: ビルド メトリクスとルーティング情報を収集する
- カスタム バンドル: プラットフォーム固有の形式で出力をパッケージ化する
- ビルド検証: 出力が特定の要件を満たしていることを確認する
- ルート生成: 処理済みルート情報を使用して、プラットフォーム固有のルーティング構成を生成する
役に立ちましたか?