Commit 2557fba3 authored by Jiachi Liu's avatar Jiachi Liu

[fix] clone the config module to avoid mutation (#80573)

parent 616f6fa0
...@@ -30,6 +30,7 @@ import { normalizeZodErrors } from '../shared/lib/zod' ...@@ -30,6 +30,7 @@ import { normalizeZodErrors } from '../shared/lib/zod'
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot' import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'
import { findDir } from '../lib/find-pages-dir' import { findDir } from '../lib/find-pages-dir'
import { CanaryOnlyError, isStableBuild } from '../shared/lib/canary-only' import { CanaryOnlyError, isStableBuild } from '../shared/lib/canary-only'
import { interopDefault } from '../lib/interop-default'
export { normalizeConfig } from './config-shared' export { normalizeConfig } from './config-shared'
export type { DomainLocale, NextConfig } from './config-shared' export type { DomainLocale, NextConfig } from './config-shared'
...@@ -1192,10 +1193,12 @@ export default async function loadConfig( ...@@ -1192,10 +1193,12 @@ export default async function loadConfig(
throw err throw err
} }
const userConfig = (await normalizeConfig( // Clone a new userConfig each time to avoid mutating the original
const loadedConfig = (await normalizeConfig(
phase, phase,
userConfigModule.default || userConfigModule interopDefault(userConfigModule)
)) as NextConfig )) as NextConfig
const userConfig = cloneObject(loadedConfig) as NextConfig
if (!process.env.NEXT_MINIMAL) { if (!process.env.NEXT_MINIMAL) {
// We only validate the config against schema in non minimal mode // We only validate the config against schema in non minimal mode
...@@ -1312,7 +1315,7 @@ export default async function loadConfig( ...@@ -1312,7 +1315,7 @@ export default async function loadConfig(
userConfig.htmlLimitedBots = userConfig.htmlLimitedBots.source userConfig.htmlLimitedBots = userConfig.htmlLimitedBots.source
} }
onLoadUserConfig?.(userConfig) onLoadUserConfig?.(Object.freeze(loadedConfig))
const completeConfig = assignDefaults( const completeConfig = assignDefaults(
dir, dir,
{ {
...@@ -1401,3 +1404,22 @@ export function getConfiguredExperimentalFeatures( ...@@ -1401,3 +1404,22 @@ export function getConfiguredExperimentalFeatures(
} }
return configuredExperimentalFeatures return configuredExperimentalFeatures
} }
function cloneObject(obj: any): any {
if (obj === null || typeof obj !== 'object') {
return obj
}
if (Array.isArray(obj)) {
return obj.map(cloneObject)
}
const keys = Object.keys(obj)
if (keys.length === 0) {
return obj
}
return keys.reduce((acc, key) => {
;(acc as any)[key] = cloneObject(obj[key])
return acc
}, {})
}
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
describe('app-dir - metadata-streaming-customized-rule', () => { describe('app-dir - metadata-streaming-customized-rule', () => {
const { next } = nextTestSetup({ const { next, isNextDev } = nextTestSetup({
files: __dirname, files: __dirname,
overrideFiles: { overrideFiles: {
'next.config.js': ` 'next.config.js': `
...@@ -39,4 +39,12 @@ describe('app-dir - metadata-streaming-customized-rule', () => { ...@@ -39,4 +39,12 @@ describe('app-dir - metadata-streaming-customized-rule', () => {
expect(await $('head title').length).toBe(0) expect(await $('head title').length).toBe(0)
expect(await $('body title').length).toBe(1) expect(await $('body title').length).toBe(1)
}) })
if (isNextDev) {
it('should not have schema issue', () => {
expect(next.cliOutput).not.toContain(
'Invalid next.config.js options detected'
)
})
}
}) })
module.exports = {
experimental: {
staticGenerationRetryCount: 2,
staticGenerationMaxConcurrency: 4,
},
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment