[ColorModeProvider] ColorMode and SSR (next.js) not working very well together
See original GitHub issueDescribe the bug
When using colorMode with SSR (in this case with next.js) the pages are rendered from the server with light mode.
However on the client the pages are rendered with whatever setting is stored on localstorage by use-dark-mode
. If the value store is darkMode = true
this leads to hydration missmatches.
This causes some of the components to be rendered with light mode even if the colorMode is dark.
Check the issue by yourself on https://next-colormode-test.santialbo.now.sh (repo on santialbo/next-colormode-test).
- Set dark mode on
- Refresh the page
Expected Behavior I would expect that by default the client renders twice, a first time with light mode so that there are no missmatches and the turns into dark mode.
To Reproduce Check santialbo/next-colormode-test for a minimal reproduction of the issue.
Desktop (please complete the following information): Tried with both Firefox and Chrome.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:41
- Comments:16 (3 by maintainers)
Top GitHub Comments
A possible solution I found that does the trick.
Since NextJS’s initial render is server-side, the value read from local storage is always set to ‘light’. We can make use of this by also storing the dark mode preference value in cookies (non-http).
This way, we can grab the initial value from
App.getInitialProps
and pass it to theColorModeProvider
context when initializing it._app.js
We also have to wrap the
toggleColorMode
function to set the cookie value on change.Snippet from my
DarkModeToggle
buttonAnd I am not sure if we still need the
noflash.js
script, but I included it anyway.FYI, there has been a very interesting article written by Josh Comeau about solving Dark Mode with SSR, originally aimed at Gatsby, but which raises similar issues as Next.js: https://joshwcomeau.com/gatsby/dark-mode/