question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`loadConfig` mutates the `imageConfigDefault` object

See original GitHub issue

What 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:closed
  • Created 2 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
timneutkenscommented, Dec 13, 2021

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? 🤔

0reactions
github-actions[bot]commented, Mar 18, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found