question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ColorMode] localStorageManager hydration issues

See original GitHub issue

When not explicitly using the cookieStorageManager, localStorageManager is used by default. Maybe this is not the right approach.

Given a Next app, in all cases (Server, Static, SSG), as soon as changing the color mode once (= not the initial value defined in the theme) and then refreshing the page, you’ll always see a hydration warning or even visual discrepancies (e.g. a button still being in light mode when you’re in dark). Any conditional logic based on color mode is thus affected.

Why is this? During export/SSR/SSG, knowledge about a users localStorage is obviously impossible. Thus the resulting HTML and CSS will be based on initial values. Once the user accesses the site, React will hydrate and expect the same markup. However, localStorageManager will now no longer return undefined as during generation, and thus, a lot of the CSS will mismatch and any HTML that renders differently based on color mode.

HTML mismatches may probably be disregarded because most apps will only have a theme toggle component render differently based on the color mode). The React docs mention for this case:

If a single element’s attribute or text content is unavoidably different between the server and the client (for example, a timestamp), you may silence the warning by adding suppressHydrationWarning={true} to the element. It only works one level deep, and is intended to be an escape hatch. Don’t overuse it. Unless it’s text content, React still won’t attempt to patch it up, so it may remain inconsistent until future updates.

The CSS mismatch is an issue though as it leads to e.g. <Button /> staying in light mode when we’re in dark.

Reproduction: https://codesandbox.io/s/broken-wind-l994s?file=/pages/index.js

  • copy the url of the sandbox and open it in another tab

  • open the devtools

  • change to dark mode

  • reload, check the console

  • <Button /> and <IconButton /> staying light image

  • <IconButton /> icon value changing image

thanks for @fr3fou for making me dig deeper.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:21 (17 by maintainers)

github_iconTop GitHub Comments

5reactions
ljosberinncommented, Aug 23, 2020

Closing this since it will be hopefully resolved for good with the next release! 🎉

4reactions
ljosberinncommented, Aug 23, 2020

@nonissue You can use getServerSideProps to access the cookies too. The issue still persists, but only for static sites where neither localStorage nor cookies are available - at build time. I’ll update the docs however, to remove getInitialProps because that’s misleading and outdated.

Scratch that, apparently a different implementation came up tonight and seems to have resolved the whole thing 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Color Mode - Chakra UI
Chakra UI comes with built-in support for managing color mode in your apps. ... Tip: Chakra stores the color mode in localStorage and...
Read more >
Dark-Mode Support with Chakra-UI and Next.js - gsdev
Let's start by updating our global styles to add color-mode support. ... from hydration and supporting the user's preferred color scheme.
Read more >
Chakra UI color mode changes on page refresh, because of ...
the easiest solution that I found is by default chakra store color-mode in local storage so you can access it by using localStorage....
Read more >
Default Color Mode - Bentley - Product Documentation
Property or Record Name, Default Color Mode . Syntax . . Values, Monochrome, Output is black and white. Grayscale, DGN file colors are...
Read more >
M1 mini Plus - Portable LED Projector User Guide - ViewSonic
Then press OK on the remote control to open the selected file. File Management. /Pictures. Local Storage. Photo 1-3.png. Photo 1 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found