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 功能。
next.config.js
不會被 Webpack 或 Babel 解析。
此頁面記錄了所有可用的配置選項。
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.
bundlePagesRouterDependencies
Enable automatic dependency bundling for Pages Router
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` and `next/head`.
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.
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
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.
productionBrowserSourceMaps
Enables browser source map generation during the production build.
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.
Runtime Config
Add client and server runtime configuration to your Next.js app.
serverExternalPackages
Opt-out specific dependencies from the dependency bundling enabled by `bundlePagesRouterDependencies`.
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
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.
這對您有幫助嗎?