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 in Next.js #4557

Open
lblblong opened this issue Dec 24, 2024 · 9 comments
Open

Hydration Mismatch in Next.js #4557

lblblong opened this issue Dec 24, 2024 · 9 comments
Labels
status: waiting for author Issue with insufficient information

Comments

@lblblong
Copy link

lblblong commented Dec 24, 2024

Steps to reproduce

https://github.com/lblblong/nextjs-toolpad-demo

Current behavior

I am currently using the Toolpad library in a Next.js project (reproduction repository: nextjs-toolpad-demo). However, I am encountering a hydration mismatch error in the browser console. The error message is as follows:
image

Expected behavior

No response

Context

No response

Your environment

npx @mui/envinfo
    System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
  Binaries:
    Node: 18.19.1 - /run/user/1000/fnm_multishells/60963_1735054989551/bin/node
    npm: 10.2.4 - /run/user/1000/fnm_multishells/60963_1735054989551/bin/npm
    pnpm: 9.4.0 - /run/user/1000/fnm_multishells/60963_1735054989551/bin/pnpm
  Browsers:
    Chrome: Not Found
  npmPackages:
    @emotion/react: ^11.14.0 => 11.14.0 
    @emotion/styled: ^11.14.0 => 11.14.0 
    @mui/icons-material: ^6.3.0 => 6.3.0 
    @mui/material: ^6.3.0 => 6.3.0 
    @mui/material-nextjs: ^6.3.0 => 6.3.0 
    @toolpad/core: ^0.11.0 => 0.11.0 
    @types/react: ^19 => 19.0.2 
    react: ^19.0.0 => 19.0.0 
    react-dom: ^19.0.0 => 19.0.0 
    typescript: ^5 => 5.7.2 

Search keywords: Hydration mismatch next.js

@lblblong lblblong added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 24, 2024
@Dave4272-Office
Copy link

I also faced the same issue

for now the following solution helped me

<AppRouterCacheProvider
            options={{ enableCssLayer: true, key: "css", prepend: true }}
          >
    // Your Content Here
</AppRouterCacheProvider>

@lblblong
Copy link
Author

I also faced the same issue

for now the following solution helped me

<AppRouterCacheProvider
            options={{ enableCssLayer: true, key: "css", prepend: true }}
          >
    // Your Content Here
</AppRouterCacheProvider>

Thanks for sharing your solution. Unfortunately, it didn't work for me. I tried implementing the with the same options you mentioned, but it still didn't resolve the issue. If you have any other suggestions, please let me know. thx.

@bharatkashyap
Copy link
Member

Try adding data-toolpad-color-scheme="light" to the html tag at the root of the layout.tsx file to prevent this from happening. The error went away for me when doing it in the layout.tsx file in your reproduction repo.

@bharatkashyap bharatkashyap added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 25, 2024
@lblblong
Copy link
Author

Try adding data-toolpad-color-scheme="light" to the html tag at the root of the layout.tsx file to prevent this from happening. The error went away for me when doing it in the layout.tsx file in your reproduction repo.Try adding data-toolpad-color-scheme="light" to the html tag at the root of the layout.tsx file to prevent this from happening. The error went away for me when doing it in the layout.tsx file in your reproduction repo.尝试将data-toolpad-color-scheme="light"添加到layout.tsx文件根目录下的html标记以防止这种情况发生。在复制存储库中的layout.tsx文件中执行此操作时,错误对我来说消失了。

I tried, but the console still reports the same error message.
https://codesandbox.io/p/github/lblblong/nextjs-toolpad-demo/main?import=true

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Dec 25, 2024
@bharatkashyap
Copy link
Member

Try adding data-toolpad-color-scheme="light" to the html tag at the root of the layout.tsx file to prevent this from happening. The error went away for me when doing it in the layout.tsx file in your reproduction repo.Try adding data-toolpad-color-scheme="light" to the html tag at the root of the layout.tsx file to prevent this from happening. The error went away for me when doing it in the layout.tsx file in your reproduction repo.尝试将data-toolpad-color-scheme="light"添加到layout.tsx文件根目录下的html标记以防止这种情况发生。在复制存储库中的layout.tsx文件中执行此操作时,错误对我来说消失了。

I tried, but the console still reports the same error message. https://codesandbox.io/p/github/lblblong/nextjs-toolpad-demo/main?import=true

For reference:

hydration.mov

@bharatkashyap bharatkashyap added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 25, 2024
@lblblong
Copy link
Author

https://codesandbox.io/p/github/lblblong/nextjs-toolpad-demo/main?import=true

It still doesn't work on my side.

20241225_182010.mp4

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Dec 25, 2024
@bharatkashyap
Copy link
Member

bharatkashyap commented Dec 25, 2024

https://codesandbox.io/p/github/lblblong/nextjs-toolpad-demo/main?import=true

It still doesn't work on my side.

20241225_182010.mp4

Got it, you need to set the value of data-toolpad-color-scheme to the theme that loads by default (during hydration), so add data-toolpad-color-scheme="dark" to the html tag instead of data-toolpad-color-scheme="light"

@bharatkashyap bharatkashyap added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 25, 2024
@lblblong
Copy link
Author

https://codesandbox.io/p/github/lblblong/nextjs-toolpad-demo/main?import=true

It still doesn't work on my side.
20241225_182010.mp4

Got it, you need to set the value of data-toolpad-color-scheme to the theme that loads by default (during hydration), so add data-toolpad-color-scheme="dark" to the html tag instead of data-toolpad-color-scheme="light"

Thank you for the suggestion, it is indeed effective. However, when this code runs on the server side, how can I determine whether the client's theme is light or dark?

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Dec 26, 2024
@bharatkashyap
Copy link
Member

https://codesandbox.io/p/github/lblblong/nextjs-toolpad-demo/main?import=true

It still doesn't work on my side.
20241225_182010.mp4

Got it, you need to set the value of data-toolpad-color-scheme to the theme that loads by default (during hydration), so add data-toolpad-color-scheme="dark" to the html tag instead of data-toolpad-color-scheme="light"

Thank you for the suggestion, it is indeed effective. However, when this code runs on the server side, how can I determine whether the client's theme is light or dark?

It's impossible to detect on the server whether the client's theme is light or dark :) Here's more information on this: https://mui.com/material-ui/customization/css-theme-variables/configuration/#preventing-ssr-flickering

@bharatkashyap bharatkashyap added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author Issue with insufficient information
Projects
None yet
Development

No branches or pull requests

3 participants