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,則可以使用 next.config.ts
在設定中使用 TypeScript
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.dev.org.tw/test',
nextConfig: {
async redirects() {
return [{ source: '/test', destination: '/test2', permanent: false }]
},
},
})
expect(response.status).toEqual(307)
expect(getRedirectUrl(response)).toEqual('https://nextjs.dev.org.tw/test2')
appDir
assetPrefix
authInterrupts
basePath
cacheLife
compress
crossOrigin
cssChunking
devIndicators
distDir
dynamicIO
env
eslint
expireTime
exportPathMap
generateBuildId
generateEtags
headers
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
turbo
typedRoutes
typescript
urlImports
useCache
useLightningcss
webpack
webVitalsAttribution
這個有幫助嗎?