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

Error: Hydration failed because the initial UI does not match what was rendered on the server. #7

Closed
hensansi opened this issue Sep 14, 2023 · 9 comments

Comments

@hensansi
Copy link

hensansi commented Sep 14, 2023

I was only able to reproduce this issue using cypress,

(uncaught exception)Error: Hydration failed because the initial UI does not match what was rendered on the server.
(uncaught exception)Error: Hydration failed because the initial UI does not match what was rendered on the server.
(uncaught exception)Error: Hydration failed because the initial UI does not match what was rendered on the server.

Warning: Expected server HTML to contain a matching <script> in <head>.

See more info here: https://nextjs.org/docs/messages/react-hydration-error
(uncaught exception)Error: Hydration failed because the initial UI does not match what was rendered on the server.

Warning: Expected server HTML to contain a matching <script> in <head>.

See more info here: https://nextjs.org/docs/messages/react-hydration-error
(uncaught exception)Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.

Moving <ColorSchemeScript /> inside the body tag solves the issue but I have no idea if that is the correct solution.
Removing the const metadata solves the issue too but not ideal.

https://nextjs.org/docs/app/api-reference/components/script
https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#step-3-migrating-nexthead

If needed I can try to create a basic cypress test to show the issue but it will take some time.

irevdev pushed a commit to rvise-au/rvise-ui-library that referenced this issue Mar 27, 2024
@trixobird
Copy link

any update here from any official source? This is happening to me in dev (not cypress). NextJs is the goto framework so it seems important to have it working without any issues out of the box

@sirPixieJerry
Copy link

sirPixieJerry commented Oct 30, 2024

I ran into the same problem today, but I am using Next.js v15 with React 19 and implemented Mantine in my own template. I used this template to look up things.

For any reason the in the head tag a suppressHydrationWarning={true} instead of just suppressHydrationWarning did the trick. But then the problem will disappear only in dev but not after the build. I also don't know why because it doesn't make any sense to me.

Nevertheless the e2e tests after build would continue failing. Thanks to a comment on another issue by the wonderful cypress team I found a solution.

Just wrap the ColorSchemeScript into a Suspense:

import '@mantine/core/styles.css';

import React, { Suspense } from 'react';
import { ColorSchemeScript, MantineProvider } from '@mantine/core';
import { theme } from '../theme';

export const metadata = {
  title: 'Mantine Next.js template',
  description: 'I am using Mantine with Next.js!',
};

export default function RootLayout({ children }: { children: any }) {
  return (
    <html lang="en">
      <head>
        <Suspense>
          <ColorSchemeScript />
        </Suspense>
        <link rel="shortcut icon" href="/favicon.svg" />
        <meta
          name="viewport"
          content="minimum-scale=1, initial-scale=1, width=device-width, user-scalable=no"
        />
      </head>
      <body>
        <MantineProvider theme={theme}>{children}</MantineProvider>
      </body>
    </html>
  );
}

@alvarocastro
Copy link

alvarocastro commented Nov 15, 2024

Hey @sirPixieJerry
I'm just starting a project with next.js 15 too and mantine, and your solution did nothing on my side, still getting the hydration error
image
It goes away if I just put the suppressHydrationWarning in the <html> tag, but I've never used it and feels bad to put it as "the solution".


Also downgraded react to version 18 to be able to install mantine.

@qhantom
Copy link

qhantom commented Nov 27, 2024

It goes away if I just put the suppressHydrationWarning in the tag, but I've never used it and feels bad to put it as "the solution".

Agree. Afaik it does suppress all hydration warnings even if they are legit, doesn't it?

@eraoul
Copy link

eraoul commented Nov 29, 2024

Same problem here. I can also fix it by explicitly writing data-mantine-color-scheme="light" in the html tag, but that's clearly a hack. The Suspense trick doesn't work for me, and I don't want to suppress hydration warnings in case they're legit.

@jayjay021
Copy link

upgrading to mantine v7.15 (released 4h ago) and adding <html {...mantineHtmlProps}> solves the problem for me

@rtivital
Copy link
Member

Yes, it has been updated in 7.15 release

@qhantom
Copy link

qhantom commented Dec 10, 2024

Thanks for your effort guys! 🙏 As I interpret the change, it still suppresses all hydration warnings even if they are legit, right? Would you mind sharing your thoughts on that change? Don't get me wrong, I do appreciate your work, I'm just curious. 🙂 Thanks!

@rtivital
Copy link
Member

See mantinedev/mantine#7008

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants