next.config.js 選項
Next.js 可以透過專案目錄根目錄(例如,透過 package.json
)中的 next.config.js
檔案,使用預設匯出進行設定。
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
next.config.mjs
// @ts-check
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
export default nextConfig
注意事項:目前不支援副檔名為
.cjs
、.cts
或.mts
的next.config
檔案。
以函式設定
您也可以使用函式
next.config.mjs
// @ts-check
export default (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
return nextConfig
}
非同步設定
從 Next.js 12.1.0 版開始,您可以使用非同步函式
next.config.js
// @ts-check
module.exports = async (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
return nextConfig
}
階段
phase
是載入設定的目前上下文。您可以查看可用的階段。階段可以從 next/constants
導入。
next.config.js
// @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
此功能在 Next.js canary 版本中提供。
如果您在專案中使用 TypeScript,您可以使用 next.config.ts
在您的設定中使用 TypeScript
next.config.ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
/* config options here */
}
export default nextConfig
被註釋的行是您可以放置 next.config.js
所允許的設定的地方,這些設定定義在此檔案中。
然而,所有設定皆非必要,您也無需理解每個設定的用途。您只需在本節中搜尋需要啟用或修改的功能,即可找到相關說明。
避免使用目標 Node.js 版本不支援的新 JavaScript 功能。Webpack 或 Babel 不會解析
next.config.js
。
本頁文件記錄了所有可用的設定選項。
appDir
Enable the App Router to use layouts, streaming, and more.
assetPrefix
Learn how to use the assetPrefix config option to configure your CDN.
basePath
Use `basePath` to deploy a Next.js application under a sub-path of a domain.
compress
Next.js provides gzip compression to compress rendered content and static files, it only works with the server target. Learn more about it here.
crossOrigin
Use the `crossOrigin` option to add a crossOrigin tag on the `script` tags generated by `next/script`.
cssChunking
Use the `cssChunking` option to control how CSS files are chunked in your Next.js application.
devIndicators
Optimized pages include an indicator to let you know if it's being statically optimized. You can opt-out of it here.
distDir
Set a custom build directory to use instead of the default .next directory.
env
Learn to add and access environment variables in your Next.js application at build time.
eslint
Next.js reports ESLint errors and warnings during builds by default. Learn how to opt-out of this behavior here.
expireTime
Customize stale-while-revalidate expire time for ISR enabled pages.
exportPathMap
Customize the pages that will be exported as HTML files when using `next export`.
generateBuildId
Configure the build id, which is used to identify the current build in which your application is being served.
generateEtags
Next.js will generate etags for every page by default. Learn more about how to disable etag generation here.
headers
Add custom HTTP headers to your Next.js app.
httpAgentOptions
Next.js will automatically use HTTP Keep-Alive by default. Learn more about how to disable HTTP Keep-Alive here.
images
Custom configuration for the next/image loader
cacheHandler
Configure the Next.js cache used for storing and revalidating data to use any external service like Redis, Memcached, or others.
logging
Configure how data fetches are logged to the console when running Next.js in development mode.
mdxRs
Use the new Rust compiler to compile MDX files in the App Router.
onDemandEntries
Configure how Next.js will dispose and keep in memory pages created in development.
optimizePackageImports
API Reference for optimizePackageImports Next.js Config Option
output
Next.js automatically traces which files are needed by each page to allow for easy deployment of your application. Learn how it works here.
pageExtensions
Extend the default page extensions used by Next.js when resolving pages in the Pages Router.
poweredByHeader
Next.js will add the `x-powered-by` header by default. Learn to opt-out of it here.
ppr
Learn how to enable Partial Prerendering in Next.js.
productionBrowserSourceMaps
Enables browser source map generation during the production build.
reactCompiler
Enable the React Compiler to automatically optimize component rendering.
reactMaxHeadersLength
The maximum length of the headers that are emitted by React and added to the response.
reactStrictMode
The complete Next.js runtime is now Strict Mode-compliant, learn how to opt-in
redirects
Add redirects to your Next.js app.
rewrites
Add rewrites to your Next.js app.
sassOptions
Configure Sass options.
serverActions
Configure Server Actions behavior in your Next.js application.
serverComponentsHmrCache
Configure whether fetch responses in Server Components are cached across HMR refresh requests.
serverExternalPackages
Opt-out specific dependencies from the Server Components bundling and use native Node.js `require`.
staleTimes
Learn how to override the invalidation time of the Client Router Cache.
staticGeneration*
Learn how to configure static generation in your Next.js application.
trailingSlash
Configure Next.js pages to resolve with or without a trailing slash.
transpilePackages
Automatically transpile and bundle dependencies from local packages (like monorepos) or from external dependencies (`node_modules`).
turbo
Configure Next.js with Turbopack-specific options
typedRoutes
Enable experimental support for statically typed links.
typescript
Next.js reports TypeScript errors by default. Learn to opt-out of this behavior here.
urlImports
Configure Next.js to allow importing modules from external URLs.
useLightningcss
Enable experimental support for Lightning CSS.
webpack
Learn how to customize the webpack config used by Next.js
webVitalsAttribution
Learn how to use the webVitalsAttribution option to pinpoint the source of Web Vitals issues.
這有幫助嗎?