ui: [bug]: Theme Provider creates hydration error in Next.js 15.0.1
Describe the bug
Implementing dark mode, and putting ThemeProvider into the layout is making hydration error in newest version of Next.js (15.0.1).
Affected component/components
ThemeProvider
How to reproduce
- Do npm install next-themes
- Create
ThemeProvidercomponent. - Wrap children in layout file
Codesandbox/StackBlitz link
https://ui.shadcn.com/docs/dark-mode/next
Logs
Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used
-className="dark"
-style={{color-scheme:"dark"}}
### System Info
```bash
Next.js 15.0.1
MacOS, Google Chrome
Before submitting
- I’ve made research efforts and searched the documentation
- I’ve searched for existing issues
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 96
- Comments: 36
Commits related to this issue
- fix: mut (BUG)[https://github.com/shadcn-ui/ui/issues/5552] — committed to Alicehhhmm/Discord by Alicehhhmm 2 years ago
Here is the real fix :
You have to dynamically import the theme provider from
next-themeand you can now remove thesuppressHydrationWarningfrom your<html>tagHey, as it is marked on the doc, you should add
suppressHydrationWarningon your html tag :Thanks! It seems to be the correct solution in general, although I must clarify that something different has worked better for me, understanding that it slows down rendering, in small projects I have not noticed any difference. On the other hand, using dynamic with ssr: false has generated a small flash at the start that I prefer to avoid. I am quite new to this, I am probably making mistakes, so I would like to read them.
Amazing! That’s exactly what I needed!
Thank you! You’re a true web dev warrior!
The Solution
add this before your theme return code
it should look like this
I think this will help you
That means that there is somewhere an issue, either with next-theme or with NextJs.
Now I think, I preffer having a “snapshot of the wrong theme” instead of suppressing all my hydration warning and troubleshouting during hours because nobody told me that i have put a
<div>into an<p>in development mode.But for the build, you’re right, you could just suppress hydration warnings by using env like :
And then in your
theme-provider.tsx:@TheOrcDev @Medaillek This is the real fix with next 15
Then after u can go ahead and import ur component in layout.tsx and wrap it around the body
app/layout.tsx
For anyone who doesn’t want to scroll through all the text above: Here’s what I’ve found and confirmed—among all the solutions, adding
suppressHydrationWarningto your HTML tag is the best solution for now!Solution 1:
Solution 2:
Verdict: It’s best to stick with
Solution 1for now until a proper solution is released.This solution is effective in preventing errors during SSR, but it isn’t an “elegant” approach. Dynamically loading the next-themes library can lead to undesired outcomes, such as a “snapshot of the wrong theme” at the start.
This is not the right approach, because you will create two extra renders in local development and same for production.
Still facing with this issue. Any one have better solutions?
suppressHydrationWarningis work but I think this is not a real fix.adding dynamic logic in themeProvider is also working but make another issue.
😦
just for information:
chakra-ui suggest add suppressHydrationWarning option at html tag in doc.
https://www.chakra-ui.com/docs/get-started/frameworks/next-app
Thank you!
So this is not connected to Next.js 15? It’s still that old problem we had with
ThemeProvider?okay for me there was dependency issue for farmer-motion with next js 15 - react 19
also weirdly could not find the
ThemeProviderPropsimport { type ThemeProviderProps } from "next-themes/dist/types";So, I downgrade
"next-themes": "^0.4.3",to"next-themes": "^0.3.0",no hydration error now !!! happy 😁
A simple fix, no
suppressHydrationWarning, no dynamic import.Disable theme related hydration warnings on purpose for dev and render immediatly for prod.
next-theme-provider.tsxExample usage
But it blinking on reload the page…(
Actually, I do have this on my project already. But I was able to fix it now, adding the “suppressHydrationWarning” on my html tag.
It did fix the hydration error for me but it caused this:
Not sure how will this affect the SEO of my page?
The
suppressHydrationWarningwon’t work for deeper server-client differences like a Theme switcher:Disabling ssr will lead to a flash which is not a fix but rather a reshaped issue.
Does anyone know why the current theme is not available when ssr?