next.js: Hydration error when using Turbopack with dynamically imported named exports.

Link to the code that reproduces this issue

https://github.com/gurkerl83/next-turbo-dynamic-import

To Reproduce

  1. Clone the reproduction repository:
git clone https://github.com/gurkerl83/next-turbo-dynamic-import.git
  1. Install dependencies using pnpm:
pnpm install
  1. Run the development server with Turbopack:
pnpm dev:turbo
  1. Observe the console for hydration issues.

Current vs. Expected behavior

Current Behavior:

  • Webpack (pnpm dev): The application runs without issues, and both dynamically imported components (direct import and named export import) render as expected.
  • Turbopack (pnpm dev:turbo): The component that uses a named export (module.SimpleComponent) results in a hydration error. The browser console shows a hydration mismatch between the server-rendered HTML and the client-side React tree.

Expected Behavior:

  • The components should hydrate correctly with both Webpack and Turbopack.
  • No hydration errors should occur when using dynamically imported named exports.

Examples:

  1. Works with both Webpack and Turbopack (direct file path import)
const ComponentDirectImport = dynamic(
  () => import("@module/first/src/SimpleComponent")
);
  1. Works with Webpack but causes hydration error with Turbopack (dynamic import with named export)
const ComponentNamedImportWithIssue = dynamic(() =>
  import("@module/first").then((module) => module.SimpleComponent)
);

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:17:33 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6031
  Available memory (MB): 65536
  Available CPU cores: 16
Binaries:
  Node: 22.6.0
  npm: 10.8.2
  Yarn: N/A
  pnpm: 9.11.0
Relevant Packages:
  next: 15.0.0-canary.177 // Latest available version is detected (15.0.0-canary.177).
  react: 19.0.0-rc-0751fac7-20241002
  react-dom: 19.0.0-rc-0751fac7-20241002
  typescript: 5.6.2
Next.js Config:
  output: N/A

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

Lazy Loading, Pages Router, Turbopack

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

next dev (local)

Additional context

  • The hydration error occurs only when using Turbopack with dynamically imported named exports.
  • The problem is triggered when using .then() after a dynamic import to access a named export (import(“@module/first”).then((module) => module.SimpleComponent)).
  • The project uses React’s latest release candidate version (19.0.0-rc-0751fac7-20241002) and Next.js’s latest canary version (15.0.0-canary.177).
  • I tested the application against the latest canary release, also against previous versions.
  • Direct imports do not cause the same issue, which suggests that the problem is related specifically to the way Turbopack handles named exports within dynamic imports.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

@devjiwonchoi I will try with the latest canary version later today, and keep you updated.

@devjiwonchoi I tried with the latest canary versions of both next and react. I have updated the reproducer with these versions.

 "next": "15.0.0-canary.196",
 "react": "19.0.0-rc-77b637d6-20241016",
 "react-dom": "19.0.0-rc-77b637d6-20241016"

Putting it on default when importing like the following, also fails with the canary versions mentioned.

const ComponentNamedImportWithIssue = dynamic(() =>
  import("@module/first").then((module) => ({
    default: module.SimpleComponent,
  }))
);

The closest I could find in the set of open PRs, which maybe is related is, but this is just a guess. https://github.com/vercel/next.js/pull/71145

Can you please forward this issue to one of the engineers, who are working on the turbopack feature.

Thx!