Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hydration mismatch caused by next/dynamic when src folder and babel are used #64741

Closed
LukasBombach opened this issue Apr 18, 2024 · 5 comments · Fixed by #65177
Closed

Hydration mismatch caused by next/dynamic when src folder and babel are used #64741

LukasBombach opened this issue Apr 18, 2024 · 5 comments · Fixed by #65177
Labels
bug Issue was opened via the bug report template. Lazy Loading Related to Next.js Lazy Loading (e.g., `next/dynamic` or `React.lazy`). linear: next Confirmed issue that is tracked by the Next.js team. locked

Comments

@LukasBombach
Copy link

LukasBombach commented Apr 18, 2024

Link to the code that reproduces this issue

https://github.com/LukasBombach/next-dynamic-src-folder-babel-bug

To Reproduce

  1. yarn install
  2. yarn dev
  3. open http://localhost:3000
  4. you will get a hydration mismatch caused by a component rendered via next/hydration

Current vs. Expected behavior

In the example, I use next/dynamic to load and render a plain react component that always returns the same JSX

src/pages/index.tsx

import dynamic from "next/dynamic";

const DynamicTest = dynamic(() => import("../components/DynamicTest").then((mod) => mod.DynamicTest), {
  loading: () => <div>Loading</div>,
});

export default function Page() {
  return <DynamicTest />;
}

src/components/DynamicTest.tsx

export const DynamicTest = () => {
  return <div>Dynamic Content</div>;
};

I would expect no errors here, but this will result in a hydration mismatch:

Text content did not match. Server: "Dynamic Content" Client: "Loading"

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:41 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T8103
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: 8.9.0
Relevant Packages:
  next: 14.2.2 // Latest available version is detected (14.2.2).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.5
Next.js Config:
  output: N/A

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

Lazy Loading

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

next dev (local), next start (local), Other (Deployed)

Additional context

The hydration mismatch only occurs under these conditions

  • babel is used
  • a src folder is used

When you either remove the .babelrc from the example or move the pages and components from the src folder to the root dir, everything works alright

The bug was introduced with next 14.2.0

We did reproduce it with 14.2.0, 14.2.1 and 14.2.2.

Next 14.1.3 works fine

NEXT-3251

@LukasBombach LukasBombach added the bug Issue was opened via the bug report template. label Apr 18, 2024
@balazsorban44 balazsorban44 added the Lazy Loading Related to Next.js Lazy Loading (e.g., `next/dynamic` or `React.lazy`). label Apr 19, 2024
@adanperez
Copy link

We are also running into this issue.

@Aedric
Copy link

Aedric commented Apr 25, 2024

Also seeing this.

I think this is the change that introduced the issue:142050d#diff-3cac9d9bfe73e0619e6407f21f6fe652da0719d0ec9074ff813ad3e416d0eb1aR945

It makes sense that this would only hit us in the /src/ directory

@huozhi huozhi added the linear: next Confirmed issue that is tracked by the Next.js team. label Apr 29, 2024
huozhi added a commit that referenced this issue Apr 29, 2024
### What

* Fix `next/dynamic` with babel config and `src/` directory
* Separate the existing `next/dynamic` dev tests, make it easier to
cover more cases, add `.babelrc` + `src/` folder coverage case

### Why

* The new transform is using `src/` or root dir as the base directory
`next/dynamic` for generating relative module path as unique key for
`next/dynamic` modules. The babel one was always using the root dir
(`cwd`), which is incorrect. It should use the `src/` dir.

In the future we could make the different solution to generate the
unique id simpler, instead of using relative paths

Closes NEXT-3254
Closes NEXT-3251
Fixes #64741
@LukasBombach
Copy link
Author

Thank you!

@huozhi
Copy link
Member

huozhi commented Apr 30, 2024

It's fixed in v14.3.0-canary.31

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2024
huozhi added a commit that referenced this issue Jun 6, 2024
### What

* Fix `next/dynamic` with babel config and `src/` directory
* Separate the existing `next/dynamic` dev tests, make it easier to
cover more cases, add `.babelrc` + `src/` folder coverage case

### Why

* The new transform is using `src/` or root dir as the base directory
`next/dynamic` for generating relative module path as unique key for
`next/dynamic` modules. The babel one was always using the root dir
(`cwd`), which is incorrect. It should use the `src/` dir.

In the future we could make the different solution to generate the
unique id simpler, instead of using relative paths

Closes NEXT-3254
Closes NEXT-3251
Fixes #64741
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Lazy Loading Related to Next.js Lazy Loading (e.g., `next/dynamic` or `React.lazy`). linear: next Confirmed issue that is tracked by the Next.js team. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants