@@ -30,6 +30,7 @@ import { normalizeZodErrors } from '../shared/lib/zod'
30
30
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'
31
31
import { findDir } from '../lib/find-pages-dir'
32
32
import { CanaryOnlyError , isStableBuild } from '../shared/lib/canary-only'
33
+ import { interopDefault } from '../lib/interop-default'
33
34
34
35
export { normalizeConfig } from './config-shared'
35
36
export type { DomainLocale , NextConfig } from './config-shared'
@@ -1192,10 +1193,12 @@ export default async function loadConfig(
1192
1193
throw err
1193
1194
}
1194
1195
1195
- const userConfig = ( await normalizeConfig (
1196
+ // Clone a new userConfig each time to avoid mutating the original
1197
+ const loadedConfig = ( await normalizeConfig (
1196
1198
phase ,
1197
- userConfigModule . default || userConfigModule
1199
+ interopDefault ( userConfigModule )
1198
1200
) ) as NextConfig
1201
+ const userConfig = cloneObject ( loadedConfig ) as NextConfig
1199
1202
1200
1203
if ( ! process . env . NEXT_MINIMAL ) {
1201
1204
// We only validate the config against schema in non minimal mode
@@ -1312,7 +1315,7 @@ export default async function loadConfig(
1312
1315
userConfig . htmlLimitedBots = userConfig . htmlLimitedBots . source
1313
1316
}
1314
1317
1315
- onLoadUserConfig ?.( userConfig )
1318
+ onLoadUserConfig ?.( Object . freeze ( loadedConfig ) )
1316
1319
const completeConfig = assignDefaults (
1317
1320
dir ,
1318
1321
{
@@ -1401,3 +1404,22 @@ export function getConfiguredExperimentalFeatures(
1401
1404
}
1402
1405
return configuredExperimentalFeatures
1403
1406
}
1407
+
1408
+ function cloneObject ( obj : any ) : any {
1409
+ if ( obj === null || typeof obj !== 'object' ) {
1410
+ return obj
1411
+ }
1412
+
1413
+ if ( Array . isArray ( obj ) ) {
1414
+ return obj . map ( cloneObject )
1415
+ }
1416
+ const keys = Object . keys ( obj )
1417
+ if ( keys . length === 0 ) {
1418
+ return obj
1419
+ }
1420
+
1421
+ return keys . reduce ( ( acc , key ) => {
1422
+ ; ( acc as any ) [ key ] = cloneObject ( obj [ key ] )
1423
+ return acc
1424
+ } , { } )
1425
+ }
0 commit comments