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

fix(theme): fix useColorMode().colorMode leading to React hydration mismatches #10954

Merged
merged 1 commit into from
Feb 27, 2025

Conversation

slorber
Copy link
Collaborator

@slorber slorber commented Feb 27, 2025

Motivation

It's not safe to init useState(getPersistedColorModeIfBrowser()), and can lead to React hydration mismatches.

We notably had one visible in docusaurus build --dev when loading a page in dark mode:

ColorModeToggle

+                               title="Switch between dark and light mode (currently dark mode)"
-                               title="Switch between dark and light mode (currently light mode)"
+                               aria-label="Switch between dark and light mode (currently dark mode)"
-                               aria-label="Switch between dark and light mode (currently light mode)"
                                aria-live="polite"
+                               aria-pressed="true"
-                               aria-pressed="false"

Using this code is safe and prevents hydration mismatches by using the exact same value that was used during SSG.

const [state, setState] = useState(defaultMode);

useEffect(() => {
  setState(getPersistedColorModeIfBrowser());
},[])

Yes the state lags behind, but this is on purpose. Related to #7986

If users need to know the color mode without lag, they can't use that in their React render code or they'll create themselves hydration errors.

If they really know what they are doing and need a fresh value, they can read document.documentElement.getAttribute('data-theme') directly, but we don't want to encourage that pattern as it's unsafe to do so.

Test Plan

CI + preview

Test links

https://deploy-preview-10954--docusaurus-2.netlify.app/

@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Feb 27, 2025
@slorber slorber requested a review from Josh-Cena as a code owner February 27, 2025 14:55
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Feb 27, 2025
Copy link

netlify bot commented Feb 27, 2025

[V2]

Name Link
🔨 Latest commit a30b5c3
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/67c07ce0c469d70008accc9e
😎 Deploy Preview https://deploy-preview-10954--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO Report
/ 🟠 70 🟢 98 🟢 96 🟢 100 Report
/docs/installation 🟠 51 🟢 97 🟢 100 🟢 100 Report
/docs/category/getting-started 🟠 73 🟢 100 🟢 100 🟠 86 Report
/blog 🟠 61 🟢 96 🟢 100 🟠 86 Report
/blog/preparing-your-site-for-docusaurus-v3 🟠 66 🟢 92 🟢 100 🟢 100 Report
/blog/tags/release 🟠 60 🟢 96 🟢 100 🟠 86 Report
/blog/tags 🟠 72 🟢 100 🟢 100 🟠 86 Report

Copy link

Size Change: +423 B (0%)

Total Size: 11.6 MB

Filename Size Change
website/build/assets/js/main.********.js 794 kB +114 B (+0.01%)
ℹ️ View Unchanged
Filename Size Change
website/.docusaurus/codeTranslations.json 2 B 0 B
website/.docusaurus/docusaurus.config.mjs 28.4 kB 0 B
website/.docusaurus/globalData.json 31.9 kB 0 B
website/.docusaurus/i18n.json 930 B 0 B
website/.docusaurus/registry.js 162 kB 0 B
website/.docusaurus/routes.js 77.8 kB 0 B
website/.docusaurus/routesChunkNames.json 85.4 kB 0 B
website/.docusaurus/site-metadata.json 2.3 kB 0 B
website/build/assets/css/styles.********.css 120 kB 0 B
website/build/assets/js/runtime~main.********.js 36.4 kB 0 B
website/build/blog.html 78 kB 0 B
website/build/blog/2017/12/14/introducing-docusaurus.html 66.4 kB 0 B
website/build/blog/2018/04/30/How-I-Converted-Profilo-To-Docusaurus.html 45.8 kB +12 B (+0.03%)
website/build/blog/2018/09/11/Towards-Docusaurus-2.html 50.2 kB 0 B
website/build/blog/2018/12/14/Happy-First-Birthday-Slash.html 30 kB 0 B
website/build/blog/2019/12/30/docusaurus-2019-recap.html 40.8 kB -1 B (0%)
website/build/blog/2020/01/07/tribute-to-endi.html 33.6 kB 0 B
website/build/blog/2021/01/19/docusaurus-2020-recap.html 51.9 kB 0 B
website/build/blog/2021/03/09/releasing-docusaurus-i18n.html 48.1 kB -1 B (0%)
website/build/blog/2021/05/12/announcing-docusaurus-two-beta.html 49 kB 0 B
website/build/blog/2021/11/21/algolia-docsearch-migration.html 55.4 kB +12 B (+0.02%)
website/build/blog/2022/01/24/docusaurus-2021-recap.html 44.2 kB 0 B
website/build/blog/2022/08/01/announcing-docusaurus-2.0.html 135 kB 0 B
website/build/blog/2022/09/01/docusaurus-2.1.html 51.6 kB -1 B (0%)
website/build/blog/archive.html 24.3 kB 0 B
website/build/blog/authors.html 51.6 kB 0 B
website/build/blog/authors/j-marcey.html 71.6 kB 0 B
website/build/blog/authors/josh-cena.html 47.9 kB 0 B
website/build/blog/authors/lex-111.html 56.9 kB 0 B
website/build/blog/authors/slorber.html 81.5 kB 0 B
website/build/blog/authors/slorber/authors/2.html 85.1 kB 0 B
website/build/blog/authors/slorber/authors/3.html 92.7 kB 0 B
website/build/blog/authors/slorber/authors/4.html 79 kB 0 B
website/build/blog/authors/yangshun.html 67.6 kB 0 B
website/build/blog/authors/zpao.html 48.4 kB 0 B
website/build/blog/page/2.html 82.3 kB 0 B
website/build/blog/page/3.html 90.2 kB 0 B
website/build/blog/page/4.html 79.3 kB 0 B
website/build/blog/page/5.html 48.4 kB 0 B
website/build/blog/page/6.html 30.7 kB 0 B
website/build/blog/preparing-your-site-for-docusaurus-v3.html 129 kB -1 B (0%)
website/build/blog/releases/2.2.html 51.5 kB 0 B
website/build/blog/releases/2.3.html 62.1 kB 0 B
website/build/blog/releases/2.4.html 64.9 kB 0 B
website/build/blog/releases/3.0.html 108 kB +12 B (+0.01%)
website/build/blog/releases/3.1.html 54.4 kB +1 B (0%)
website/build/blog/releases/3.2.html 50.1 kB 0 B
website/build/blog/releases/3.3.html 57.3 kB 0 B
website/build/blog/releases/3.4.html 56.5 kB +7 B (+0.01%)
website/build/blog/releases/3.5.html 58.9 kB +1 B (0%)
website/build/blog/releases/3.6.html 76.8 kB +1 B (0%)
website/build/blog/releases/3.7.html 51.8 kB +1 B (0%)
website/build/blog/tags.html 28.1 kB 0 B
website/build/blog/upgrading-frontend-dependencies-with-confidence-using-visual-regression-testing.html 126 kB 0 B
website/build/docs.html 48.5 kB 0 B
website/build/docs/advanced.html 30.3 kB 0 B
website/build/docs/advanced/architecture.html 29.8 kB +4 B (+0.01%)
website/build/docs/advanced/client.html 70 kB 0 B
website/build/docs/advanced/plugins.html 54.9 kB 0 B
website/build/docs/advanced/routing.html 70.2 kB +8 B (+0.01%)
website/build/docs/advanced/ssg.html 74.9 kB 0 B
website/build/docs/api/docusaurus-config.html 189 kB 0 B
website/build/docs/api/misc/@docusaurus/eslint-plugin.html 44 kB +4 B (+0.01%)
website/build/docs/api/misc/@docusaurus/eslint-plugin/no-html-links.html 36.1 kB 0 B
website/build/docs/api/misc/@docusaurus/eslint-plugin/no-untranslated-text.html 35 kB 0 B
website/build/docs/api/misc/@docusaurus/eslint-plugin/prefer-docusaurus-heading.html 36.3 kB +4 B (+0.01%)
website/build/docs/api/misc/@docusaurus/eslint-plugin/string-literal-i18n-messages.html 40 kB 0 B
website/build/docs/api/misc/@docusaurus/logger.html 38.5 kB 0 B
website/build/docs/api/misc/create-docusaurus.html 33.5 kB 0 B
website/build/docs/api/misc/docusaurus-init/index.html 361 B 0 B
website/build/docs/api/plugin-methods.html 65.5 kB +4 B (+0.01%)
website/build/docs/api/plugin-methods/extend-infrastructure.html 59.4 kB +4 B (+0.01%)
website/build/docs/api/plugin-methods/i18n-lifecycles.html 57.6 kB +4 B (+0.01%)
website/build/docs/api/plugin-methods/lifecycle-apis.html 162 kB 0 B
website/build/docs/api/plugin-methods/static-methods.html 44.6 kB +4 B (+0.01%)
website/build/docs/api/plugins.html 31.6 kB 0 B
website/build/docs/api/plugins/@docusaurus/plugin-client-redirects.html 58.8 kB +13 B (+0.02%)
website/build/docs/api/plugins/@docusaurus/plugin-content-blog.html 187 kB +1 B (0%)
website/build/docs/api/plugins/@docusaurus/plugin-content-docs.html 193 kB +1 B (0%)
website/build/docs/api/plugins/@docusaurus/plugin-content-pages.html 73.1 kB 0 B
website/build/docs/api/plugins/@docusaurus/plugin-debug.html 46.7 kB +12 B (+0.03%)
website/build/docs/api/plugins/@docusaurus/plugin-google-analytics.html 48.3 kB +3 B (+0.01%)
website/build/docs/api/plugins/@docusaurus/plugin-google-gtag.html 47.8 kB -4 B (-0.01%)
website/build/docs/api/plugins/@docusaurus/plugin-google-tag-manager.html 46.5 kB 0 B
website/build/docs/api/plugins/@docusaurus/plugin-ideal-image.html 47.9 kB +4 B (+0.01%)
website/build/docs/api/plugins/@docusaurus/plugin-pwa.html 117 kB +4 B (0%)
website/build/docs/api/plugins/@docusaurus/plugin-rsdoctor.html 39.2 kB 0 B
website/build/docs/api/plugins/@docusaurus/plugin-sitemap.html 64.7 kB +6 B (+0.01%)
website/build/docs/api/plugins/@docusaurus/plugin-svgr.html 44.3 kB 0 B
website/build/docs/api/plugins/@docusaurus/plugin-vercel-analytics.html 39.6 kB +12 B (+0.03%)
website/build/docs/api/themes.html 29.6 kB 0 B
website/build/docs/api/themes/@docusaurus/theme-classic.html 42.7 kB +4 B (+0.01%)
website/build/docs/api/themes/@docusaurus/theme-live-codeblock.html 35.7 kB +2 B (+0.01%)
website/build/docs/api/themes/@docusaurus/theme-mermaid.html 34.4 kB +4 B (+0.01%)
website/build/docs/api/themes/@docusaurus/theme-search-algolia.html 32.2 kB 0 B
website/build/docs/api/themes/configuration.html 241 kB -2 B (0%)
website/build/docs/blog.html 200 kB +6 B (0%)
website/build/docs/browser-support.html 47.2 kB 0 B
website/build/docs/category/getting-started.html 27.5 kB 0 B
website/build/docs/category/guides.html 35.6 kB 0 B
website/build/docs/cli.html 58.5 kB 0 B
website/build/docs/configuration.html 92 kB +7 B (+0.01%)
website/build/docs/create-doc.html 59.2 kB +12 B (+0.02%)
website/build/docs/creating-pages.html 56.5 kB +1 B (0%)
website/build/docs/deployment.html 188 kB +2 B (0%)
website/build/docs/docs-introduction.html 49.8 kB 0 B
website/build/docs/docs-multi-instance.html 70.9 kB +4 B (+0.01%)
website/build/docs/docusaurus-core.html 226 kB 0 B
website/build/docs/guides/whats-next.html 32 kB 0 B
website/build/docs/i18n/crowdin.html 134 kB +12 B (+0.01%)
website/build/docs/i18n/git.html 72.5 kB +4 B (+0.01%)
website/build/docs/i18n/introduction.html 48.1 kB 0 B
website/build/docs/i18n/tutorial.html 155 kB +13 B (+0.01%)
website/build/docs/installation.html 62 kB +5 B (+0.01%)
website/build/docs/introduction/index.html 280 B 0 B
website/build/docs/markdown-features.html 78.8 kB 0 B
website/build/docs/markdown-features/admonitions.html 111 kB +4 B (0%)
website/build/docs/markdown-features/assets.html 86.4 kB +2 B (0%)
website/build/docs/markdown-features/code-blocks.html 217 kB 0 B
website/build/docs/markdown-features/diagrams.html 50.5 kB +14 B (+0.03%)
website/build/docs/markdown-features/head-metadata.html 50.1 kB 0 B
website/build/docs/markdown-features/links.html 40.4 kB 0 B
website/build/docs/markdown-features/math-equations.html 88.8 kB +4 B (0%)
website/build/docs/markdown-features/plugins.html 94.3 kB +8 B (+0.01%)
website/build/docs/markdown-features/react.html 133 kB 0 B
website/build/docs/markdown-features/tabs.html 141 kB 0 B
website/build/docs/markdown-features/toc.html 82.1 kB +4 B (0%)
website/build/docs/migration.html 37.6 kB -1 B (0%)
website/build/docs/migration/v2.html 39.1 kB 0 B
website/build/docs/migration/v2/automated.html 38.8 kB 0 B
website/build/docs/migration/v2/manual.html 189 kB 0 B
website/build/docs/migration/v2/translated-sites.html 50.7 kB 0 B
website/build/docs/migration/v2/versioned-sites.html 60 kB +8 B (+0.01%)
website/build/docs/migration/v3.html 191 kB +2 B (0%)
website/build/docs/playground.html 31.7 kB 0 B
website/build/docs/resources/index.html 325 B 0 B
website/build/docs/search.html 111 kB +1 B (0%)
website/build/docs/seo.html 88.1 kB +13 B (+0.01%)
website/build/docs/sidebar.html 130 kB +7 B (+0.01%)
website/build/docs/sidebar/autogenerated.html 145 kB +13 B (+0.01%)
website/build/docs/sidebar/items.html 172 kB 0 B
website/build/docs/sidebar/multiple-sidebars.html 61.5 kB +4 B (+0.01%)
website/build/docs/static-assets.html 51.4 kB +4 B (+0.01%)
website/build/docs/styling-layout.html 135 kB +1 B (0%)
website/build/docs/support/index.html 319 B 0 B
website/build/docs/swizzling.html 105 kB +14 B (+0.01%)
website/build/docs/team/index.html 310 B 0 B
website/build/docs/typescript-support.html 58.9 kB +4 B (+0.01%)
website/build/docs/using-plugins.html 105 kB 0 B
website/build/docs/versioning.html 90.4 kB -2 B (0%)
website/build/index.html 36.9 kB 0 B

compressed-size-action::DOCUSAURUS_INFRA_SLOWER

@slorber slorber added the Argos Add this label to run UI visual regression tests. See argos.yml GH action. label Feb 27, 2025
Copy link

argos-ci bot commented Feb 27, 2025

The latest updates on your projects. Learn more about Argos notifications ↗︎

Build Status Details Updated (UTC)
default (Inspect) 👍 Changes approved 1 changed Feb 27, 2025, 3:18 PM

@slorber slorber merged commit 396deed into main Feb 27, 2025
41 of 42 checks passed
@slorber slorber deleted the slorber/fix-color-mode-hydration-mismatch branch February 27, 2025 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Argos Add this label to run UI visual regression tests. See argos.yml GH action. CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants