Next Dynamic doesn't work as expected with swcTransform

Open
#60,174 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale

Research direction

Start with the linked dynamic-import-issue reproduction: run yarn install and yarn dev, then compare forceSwcTransforms in next.config.js with and without babel.config.js. Inspect the dynamic import of components/Header and verify that the Hello chunk loads only after clicking Show Header under SWC, matching the existing Babel behavior.

Written by the indexing model from the issue text.

Description

bug SWC
Link to the code that reproduces this issue

https://github.com/milap1296/dynamic-import-issue

To Reproduce
  1. yarn install.
  2. yarn dev.
  3. Set forceSwcTransforms: true in next.config.js.
  4. See that the dynamically loaded component is preloaded when swc transpilation is enabled.
  5. It is not preloaded when forceSwcTransforms: false is set and works as expected with babel transpilation.
Current vs. Expected behavior

Current Behavior
Click on the Show Header button and the chunk corresponding to Hello should only be rendered then. It works as expected when forceSwcTransforms is false but when forceSwcTransforms is true, it preloads the Hello even before we click the Show Header button.
image

The underlying issue here is with this particular code - dynamic(import('../components/Header'), it works as expected with swc if I replace it with dynamic(() => import('../components/Header'), but what should be the ideal behavior because there are many instances of such in my app.

Expected Behavior
Lazy Loading should work as expected when forceSwcTransforms is true. This also doesn't work when the babel.config.js file is not present and we don't have any experimental configuration as well.

Verify canary release
  • I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
Binaries:
  Node: 18.18.2
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.0.4
  eslint-config-next: 14.0.4
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config: {
    swcMinify: true,
    reactStrictMode: true
}
Which area(s) are affected? (Select all that apply)

SWC transpilation

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), next start (local)

Additional context

No response

Dominant language
JavaScript
Stars
142k
Forks
32.5k
Avg merge
2d 13h
Merged PRs (30d)
334

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vercel/next.js

All issues in vercel/next.js

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.