Skip to content

Conversation

@martenbjork
Copy link
Contributor

@martenbjork martenbjork commented Dec 15, 2022

Fixes this annoying message in development:

image

It was caused by the dark mode switcher having different states between the server and the client, thus breaking rehydration. This fix ensures that the dark mode toggle is only rendered on the client.

@martenbjork martenbjork added the 🤖Skip Changelog Causes CI to ignore changelog update check. label Dec 15, 2022
@martenbjork martenbjork changed the title Fix next.js rehydration issue [polaris.shopify.com] Fix next.js rehydration issue Dec 15, 2022
Copy link
Member

@kyledurand kyledurand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, didn't tophat but put some recommendations on a11y improvements

{isMounted && (
<button className={styles.DarkModeToggle} onClick={darkMode.toggle}>
{darkMode.value ? (
<span className={styles.LightModeIcon}>💡</span>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<span className={styles.LightModeIcon}>💡</span>
<span className={styles.LightModeIcon} role="img" aria-label="toggle light mode">💡</span>

{darkMode.value ? (
<span className={styles.LightModeIcon}>💡</span>
) : (
<span className={styles.DarkModeIcon}>🌙</span>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<span className={styles.DarkModeIcon}>🌙</span>
<span className={styles.DarkModeIcon} role="img" aria-label="toggle dark mode">🌙</span>

Copy link
Contributor

@laurkim laurkim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tophatted locally and can confirm the errors go away 💯 🌟

@martenbjork
Copy link
Contributor Author

Tophatted locally and can confirm the errors go away 💯 🌟

Sweet! Thanks, Lo! 🚀

@martenbjork martenbjork merged commit 5b5177d into main Dec 15, 2022
@martenbjork martenbjork deleted the fix-localhost-rehydration-issue branch December 15, 2022 15:55
@gwyneplaine gwyneplaine mentioned this pull request Feb 14, 2023
juzser pushed a commit to juzser/polaris that referenced this pull request Jul 27, 2023
Fixes this annoying message in development:

<img width="1011" alt="image"
src="https://user-images.githubusercontent.com/875708/207904298-8ebb19c3-3b9d-43cf-8a1a-78939a3f6b1a.png">

It was caused by the dark mode switcher having different states between
the server and the client, thus breaking rehydration. This fix ensures
that the dark mode toggle is only rendered on the client.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖Skip Changelog Causes CI to ignore changelog update check.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants