next.config.js
Next.js 可以通过在项目根目录(例如 package.json 所在目录)下的 next.config.js 文件,以默认导出的方式进行配置。
// @ts-check
/** @type {import('next').NextConfig} */
const nextConfig = {
/* config options here */
}
module.exports = nextConfigECMAScript 模块
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.net.cn/test',
nextConfig: {
async redirects() {
return [{ source: '/test', destination: '/test2', permanent: false }]
},
},
})
expect(response.status).toEqual(307)
expect(getRedirectUrl(response)).toEqual('https://nextjs.net.cn/test2')experimental.adapterPath
allowedDevOrigins
appDir
assetPrefix
authInterrupts
basePath
browserDebugInfoInTerminal
cacheComponents
cacheLife
compress
crossOrigin
cssChunking
devIndicators
distDir
env
expireTime
exportPathMap
generateBuildId
generateEtags
headers
htmlLimitedBots
httpAgentOptions
images
cacheHandler
inlineCss
isolatedDevBuild
logging
mdxRs
onDemandEntries
optimizePackageImports
output
pageExtensions
poweredByHeader
productionBrowserSourceMaps
proxyClientMaxBodySize
reactCompiler
reactMaxHeadersLength
reactStrictMode
redirects
rewrites
sassOptions
serverActions
serverComponentsHmrCache
serverExternalPackages
staleTimes
staticGeneration*
taint
trailingSlash
transpilePackages
turbopack
turbopackFileSystemCache
typedRoutes
typescript
urlImports
useLightningcss
viewTransition
webpack
webVitalsAttribution
这有帮助吗?