`loadConfig` mutates the `imageConfigDefault` object
See original GitHub issueWhat version of Next.js are you using?
12.0.7
What version of Node.js are you using?
16.13.1
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
next start
Describe the Bug
Inside of assignDefaults
function we merge and copy defaultConfig
with the passed in config
.
// https://github.com/vercel/next.js/blob/v12.0.7/packages/next/server/config.ts#L14
const result = { ...defaultConfig, ...config }
Then later mutates result
by assigning sub-properties.
// https://github.com/vercel/next.js/blob/v12.0.7/packages/next/server/config.ts#L205
images.domains.push(new URL(config.assetPrefix).hostname)
// https://github.com/vercel/next.js/blob/v12.0.7/packages/next/server/config.ts#L279
images.loader = 'default'
// https://github.com/vercel/next.js/blob/v12.0.7/packages/next/server/config.ts#L314
images.path = `${result.basePath}${images.path}`
Because the merge and copy is shallow, when we mutate result.images
, we are actually mutating the original object imageConfigDefault
exported from https://github.com/vercel/next.js/blob/v12.0.7/packages/next/server/image-config.ts.
This could cause a number of issues, for example, in the case I’m running into, every time loadConfig
is called the basePath
property is pre-pended to imageConfigDefault.path
.
console.log(imageConfigDefault.path)
// "/_next/image"
await loadConfig("phase-development-server", "./path/to/config/with/foo/base/path");
await loadConfig("phase-development-server", "./path/to/config/with/foo/base/path");
await loadConfig("phase-development-server", "./path/to/config/with/foo/base/path");
console.log(imageConfigDefault.path)
// "/foo/foo/foo/_next/image"
See the codesandbox for an example.
Expected Behavior
Calling loadConfig
with a config that has a basePath
should not mutate imageConfigDefault
or defaultConfig
.
To Reproduce
https://codesandbox.io/s/immutable-thunder-g3bj8?file=/server.js
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (4 by maintainers)
Top GitHub Comments
So this is indeed unexpected, however
loadConfig
is only ever called once so this would indicate that you’re using internals in some way, what are you doing exactly? 🤔This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.