next.config.js
Next.jsは、プロジェクトディレクトリのルート(例:package.json
の横)にnext.config.js
ファイルを配置し、デフォルトエクスポートを行うことで設定できます。
// @ts-check
/** @type {import('next').NextConfig} */
const nextConfig = {
/* config options here */
}
module.exports = nextConfig
ECMAScriptモジュール
next.config.js
は通常のNode.jsモジュールであり、JSONファイルではありません。Next.jsサーバーとビルドフェーズで使用され、ブラウザビルドには含まれません。
ECMAScriptモジュールが必要な場合は、next.config.mjs
を使用できます。
// @ts-check
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
export default nextConfig
注意点:
.cjs
、.cts
、または.mts
拡張子のnext.config
は現在サポートされていません。
関数としての設定
関数を使用することもできます
// @ts-check
export default (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
return nextConfig
}
非同期設定
Next.js 12.1.0以降、非同期関数を使用できます。
// @ts-check
module.exports = async (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
return nextConfig
}
フェーズ
phase
は、設定がロードされる現在のコンテキストです。利用可能なフェーズを確認できます。フェーズはnext/constants
からインポートできます。
// @ts-check
const { PHASE_DEVELOPMENT_SERVER } = require('next/constants')
module.exports = (phase, { defaultConfig }) => {
if (phase === PHASE_DEVELOPMENT_SERVER) {
return {
/* development only config options here */
}
}
return {
/* config options for all phases except development here */
}
}
TypeScript
プロジェクトでTypeScriptを使用している場合、設定でTypeScriptを使用するためにnext.config.ts
を使用できます。
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
/* config options here */
}
export default nextConfig
コメントアウトされた行は、next.config.js
で許可されている設定を記述できる場所であり、それらはこのファイルで定義されています。
ただし、すべての設定が必須というわけではなく、各設定が何をするのかを理解する必要はありません。代わりに、このセクションで有効または変更したい機能を検索すると、その方法が表示されます。
対象のNode.jsバージョンで利用できない新しいJavaScript機能を使用することは避けてください。
next.config.js
はWebpackやBabelによってパースされません。
このページでは、利用可能なすべての設定オプションを文書化しています。
単体テスト (実験的)
Next.js 15.1以降、next/experimental/testing/server
パッケージには、next.config.js
ファイルの単体テストを支援するユーティリティが含まれています。
unstable_getResponseFromNextConfig
関数は、提供されたリクエスト情報を使用してnext.config.js
のheaders
、redirects
、およびrewrites
関数を実行し、ルーティング結果を含むNextResponse
を返します。
unstable_getResponseFromNextConfig
からの応答は、next.config.js
のフィールドのみを考慮し、ミドルウェアやファイルシステムルートは考慮しないため、本番環境での結果は単体テストと異なる場合があります。
import {
getRedirectUrl,
unstable_getResponseFromNextConfig,
} from 'next/experimental/testing/server'
const response = await unstable_getResponseFromNextConfig({
url: 'https://nextjs.dokyumento.jp/test',
nextConfig: {
async redirects() {
return [{ source: '/test', destination: '/test2', permanent: false }]
},
},
})
expect(response.status).toEqual(307)
expect(getRedirectUrl(response)).toEqual('https://nextjs.dokyumento.jp/test2')
allowedDevOrigins
appDir
assetPrefix
authInterrupts
basePath
cacheLife
compress
crossOrigin
cssChunking
devIndicators
distDir
dynamicIO
env
eslint
expireTime
exportPathMap
generateBuildId
generateEtags
headers
htmlLimitedBots
httpAgentOptions
images
cacheHandler
inlineCss
logging
mdxRs
onDemandEntries
optimizePackageImports
output
pageExtensions
poweredByHeader
ppr
productionBrowserSourceMaps
reactCompiler
reactMaxHeadersLength
reactStrictMode
redirects
rewrites
sassOptions
serverActions
serverComponentsHmrCache
serverExternalPackages
staleTimes
staticGeneration*
trailingSlash
transpilePackages
turbopack
typedRoutes
typescript
urlImports
useCache
useLightningcss
ViewTransition
webpack
webVitalsAttribution
お役に立ちましたか?