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.

React 18: Hydration Issue caused by Indicator/ShadowPortal component

See original GitHub issue

Preliminary Checks

Description

When running Gatsby with React 18 (Concurrent mode) and onRecoverableError callback provided to react’s hydrateRoot/createRoot functions, I get a Error: Hydration failed because the initial UI does not match what was rendered on the server error.

The cause of this error seems to be the Indicator resp. ShadowPortal component of gatsby (included from .cache/shadow-portal.js / .cache/indicator.js), as ShadowPortal renders a span element and Indicator then renders div elements inside that span, which is invalid.

Screenshot 2022-07-03 at 00 23 28

The error occurs always in develop mode, and sometimes even in production builds.

If I disable the loading indicator (via http://localhost:8000/___loading-indicator/disable), the error doesn’t occur anymore.

Reproduction Link

https://github.com/kije/gatsby-bugreport-hydration-error-shadow-portal-indicator

Steps to Reproduce

  1. Checkout reprodution repo
  2. Start gatsby with npm run develop

Expected Result

The hydration error described above does not occur

Actual Result

Hydration error described above occurs

Environment

System:
    OS: macOS 12.3.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.4.0 - ~/.nvm/versions/node/v18.4.0/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.13.0 - ~/.nvm/versions/node/v18.4.0/bin/npm
  Languages:
    Python: 2.7.18 - /Library/Frameworks/Python.framework/Versions/2.7/bin/python
  Browsers:
    Chrome: 103.0.5060.53
    Firefox: 102.0
  npmPackages:
    gatsby: ^4.17.2 => 4.17.2
    gatsby-alias-imports: ^1.0.6 => 1.0.6
    gatsby-parcel-config: file:./.gatsby/fixed-packages/gatsby-parcel-config => 0.9.0
    gatsby-plugin-bundle-stats: ^3.3.10 => 3.3.10
    gatsby-plugin-image: ^2.17.0 => 2.17.0
    gatsby-plugin-layout: ^3.17.0 => 3.17.0
    gatsby-plugin-lodash: ^5.17.0 => 5.17.0
    gatsby-plugin-lomobox-file-downloader: file:./plugins/file-downloader => 1.0.0-rev4
    gatsby-plugin-next-seo: ^1.10.0 => 1.10.0
    gatsby-plugin-nprogress: ^4.17.0 => 4.17.0
    gatsby-plugin-offline-next: ^5.2.3 => 5.2.3
    gatsby-plugin-optimize-svgs: ^1.0.5 => 1.0.5
    gatsby-plugin-persist-cache: ^0.0.2 => 0.0.2
    gatsby-plugin-postcss: ^5.17.0 => 5.17.0
    gatsby-plugin-preconnect: ^1.3.0 => 1.3.0
    gatsby-plugin-purgecss: ^6.1.2 => 6.1.2
    gatsby-plugin-react-i18next: ^1.2.2 => 1.2.2
    gatsby-plugin-react-svg: ^3.1.0 => 3.1.0
    gatsby-plugin-realfavicongenerator: ^0.1.2 => 0.1.2
    gatsby-plugin-robots-txt: ^1.7.1 => 1.7.1
    gatsby-plugin-sharp: file:./plugins/shared/gatsby-plugin-sharp => 4.4.0-rev11
    gatsby-plugin-sitemap: ^5.17.0 => 5.17.0
    gatsby-plugin-split-css: ^2.0.2 => 2.0.2
    gatsby-plugin-styled-components: ^5.17.0 => 5.17.0
    gatsby-plugin-ts-checker: ^1.1.0 => 1.1.0
    gatsby-plugin-typegen: ^3.0.0 => 3.0.0
    gatsby-plugin-webfonts: ^2.2.2 => 2.2.2
    gatsby-plugin-webpack-bundle-analyser-v2: ^1.1.27 => 1.1.27
    gatsby-source-craft: https://github.com/kije/gatsby-source-craft.git#gatsby-v4-patched => 3.0.1
    gatsby-source-filesystem: ^4.17.0 => 4.17.0
    gatsby-source-iubenda-documents: ^2.1.0 => 2.1.0
    gatsby-transformer-remark: ^5.17.0 => 5.17.0
    gatsby-transformer-sharp: ^4.17.0 => 4.17.0
    gatsby-transformer-sqip: file:./plugins/shared/gatsby-transformer-sqip => 4.4.0-rev27
  npmGlobalPackages:
    gatsby-cli: 4.17.1

Config Flags

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tyhoppcommented, Jul 7, 2022

Hi @kije, thanks for the description and reproduction.

I tested these six scenarios with the latest Gatsby version (gatsby@4.18.0):

Not using replaceHydrateFunction (unchanged minimal starter):

  • Dev - No error
  • Dev + DEV_SSR - Hydration error
  • Prod (via gatsby serve) - No error

Using replaceHydrateFunction from reproduction:

  • Dev - Hydration error
  • Dev + DEV_SSR - Hydration error
  • Prod (via gatsby serve) - No error

It makes sense that prod doesn’t have this issue since the related code isn’t used in prod but I included anyway it since it was mentioned.

So breaking it down there are two things to fix/improve:

The second point should help shed light on why the replaceHydrationFunction usage in @kije’s reproduction repo has the error too.

0reactions
jonsadkacommented, Jul 14, 2022

Thanks for the fix @pieh!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React 18: Hydration failed because the initial UI does not ...
I have react 18.2.0 with next 12.2.4 and I fix hydration with this ... Example: adding Box component inside Iconbutton will cause an...
Read more >
react-hydration-error - Next.js
In general this issue is caused by using a specific library or application code that is relying on something that could differ between...
Read more >
Debugging and fixing hydration issues - somewhat abstract
To exclude a component that needs layout effects from the server-rendered HTML, render it conditionally with showChild && <Child /> and defer ...
Read more >
hydrate - React Docs
hydrate lets you display React components inside a browser DOM node whose HTML content was previously generated by react-dom/server in React 17 and...
Read more >
React hydration error 425 "Text content does not match server ...
If you're upgrading to React 18 and have run into the following error, ... cause a page to re-render this could lead to...
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