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

MantineProvider causes hydration error on Nextjs 15 #7008

Closed
1 of 2 tasks
davidchalifoux opened this issue Oct 21, 2024 · 12 comments
Closed
1 of 2 tasks

MantineProvider causes hydration error on Nextjs 15 #7008

davidchalifoux opened this issue Oct 21, 2024 · 12 comments

Comments

@davidchalifoux
Copy link

Dependencies check up

  • I have verified that I use latest version of all @mantine/* packages

What version of @mantine/* packages do you have in package.json?

7.13.3

What package has an issue?

@mantine/core

What framework do you use?

Next.js

In which browsers you can reproduce the issue?

Chrome

Describe the bug

Next.js just released version 15 today.
Testing Mantine with version 15 is resulting in a hydration error caused by <MantineProvider>, seemingly because the error suppression isn't working. I've included a minimal reproduction on StackBlitz.

Thanks for your work!

Screenshot 2024-10-21 at 4 50 40 PM

If possible, include a link to a codesandbox with a minimal reproduction

https://stackblitz.com/edit/stackblitz-starters-avcjyw?file=app%2Flayout.tsx

Possible fix

No response

Self-service

  • I would be willing to implement a fix for this issue
@davidchalifoux
Copy link
Author

Looks like the error emitted by Next is in a slightly different format so suppressNextjsWarning() won't catch it.

In the meantime I was able to get rid of the error by manually adding data-mantine-color-scheme="light" to <html> in my root layout so there isn't a mismatch:

<html lang="en" data-mantine-color-scheme="light">

This is fine for me because the project is only using the light scheme.

@Pirulax
Copy link

Pirulax commented Oct 21, 2024

Interesting, according to the docs data-mantine-color-scheme should be set by MantineProvider. (Source)
[As a sidenote: I can also confirm this issue, I just ran npx @next/codemod@canary upgrade latest and got it... Was about to open an issue for the nextjs template]

@AIEPhoenix
Copy link

just add suppressHydrationWarning to html tag

@VibingCreator
Copy link

Getting rid of <ColorSchemeScript /> from the head tag solves issues for me.

@alimehasin
Copy link
Contributor

This PR might solve be relevant #5199

@rtivital
Copy link
Member

To fix the issue, set suppressHydrationWarning on the html tag.

example of the full file layout.tsx in app router:

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

import React 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" suppressHydrationWarning>
      <head>
        <ColorSchemeScript />
        <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>
  );
}

Example for pages router in _document.tsx file:

import { Head, Html, Main, NextScript } from 'next/document';
import { ColorSchemeScript } from '@mantine/core';

export default function Document() {
  return (
    <Html lang="en" suppressHydrationWarning>
      <Head>
        <ColorSchemeScript />
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}

@rtivital
Copy link
Member

The issue has been resolved, the documentation has been updated to support next 15 release. All templates have been updated to next 15. You can find full working examples in the corresponding templates. Updated documentation can be found here – https://mantine.dev/guides/next/

@rtivital rtivital pinned this issue Oct 23, 2024
@matheusvellone
Copy link
Contributor

@rtivital does adding the ColorSchemeScript on body instead of head affects something? (cant test it right now)

Personaly, I would like not to supress the warnings if possible, as it might be reporting that something is possibly wrong.

@rtivital
Copy link
Member

The recommended way suppresses warning on the html element only. Hydration warning on the html element is completely useless – it can be produced by any browser extension that modifies html attributes, for example Grammarly, LanguageTool, etc.

Putting ColorSchemeScript in the body does not change anything – you get the error because an attribute is added to html element, not because there is an extra script in the head.

@Juanryhn
Copy link

Juanryhn commented Nov 9, 2024

The recommended way suppresses warning on the html element only. Hydration warning on the html element is completely useless – it can be produced by any browser extension that modifies html attributes, for example Grammarly, LanguageTool, etc.

Putting ColorSchemeScript in the body does not change anything – you get the error because an attribute is added to html element, not because there is an extra script in the head.

@rtivital I'm using next v14 and put it on body <ColorSchemeScript/> seperate it from root layout, and add use client on top of file it works fine. But when I add suppressHydration on html tag it doesn't change anything I still got the error. Do I have to upgrade next ?

@Leejha
Copy link

Leejha commented Nov 20, 2024

@rtivital
Is it better to use MantineProvider and ColorSchemScript in client components or insert suppressHydrationWarning properties in the html tag in the server component?

@rtivital
Copy link
Member

It is not relevant client or server. Follow the guide, it will work correctly.

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

9 participants