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

[rfc] Insert empty text node during hydration #22803

Closed
wants to merge 1 commit into from

Conversation

salazarm
Copy link
Contributor

Summary

When we server render an empty string from the server the browser does not create a text node for the empty string. This leads to a hydration mismatch. Since #22629 we now throw on mismatches and fallback to client render. This causes empty strings to always lead to client rendering #22784. This diff adds logic in hydration to insert an empty text node if there isn't one already when processing a fiber for an empty string text node. If we do another pass and see the empty text node again then it should be there already so we use the previously created one.

How did you test this change?

jest

@sizebot
Copy link

sizebot commented Nov 22, 2021

Comparing: 149b420...f898b8c

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js +0.35% 129.98 kB 130.43 kB +0.35% 41.56 kB 41.71 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js +0.33% 134.74 kB 135.19 kB +0.34% 42.96 kB 43.11 kB
facebook-www/ReactDOM-prod.classic.js +0.28% 424.49 kB 425.69 kB +0.33% 78.24 kB 78.49 kB
facebook-www/ReactDOM-prod.modern.js +0.29% 413.04 kB 414.25 kB +0.29% 76.50 kB 76.73 kB
facebook-www/ReactDOMForked-prod.classic.js +0.28% 424.49 kB 425.69 kB +0.33% 78.24 kB 78.50 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable-semver/react-dom/cjs/react-dom-testing.production.min.js +0.36% 124.45 kB 124.90 kB +0.29% 39.83 kB 39.95 kB
oss-stable/react-dom/cjs/react-dom-testing.production.min.js +0.36% 124.45 kB 124.90 kB +0.29% 39.83 kB 39.95 kB
oss-stable-semver/react-dom/cjs/react-dom.production.min.js +0.35% 129.98 kB 130.43 kB +0.35% 41.56 kB 41.71 kB
oss-stable/react-dom/cjs/react-dom.production.min.js +0.35% 129.98 kB 130.43 kB +0.35% 41.56 kB 41.71 kB
oss-stable-semver/react-dom/umd/react-dom.production.min.js +0.35% 130.12 kB 130.57 kB +0.29% 42.29 kB 42.42 kB
oss-stable/react-dom/umd/react-dom.production.min.js +0.35% 130.12 kB 130.57 kB +0.29% 42.29 kB 42.42 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js +0.33% 134.74 kB 135.19 kB +0.34% 42.96 kB 43.11 kB
oss-experimental/react-dom/umd/react-dom.production.min.js +0.33% 134.79 kB 135.24 kB +0.27% 43.61 kB 43.73 kB
oss-experimental/react-dom/cjs/react-dom-testing.production.min.js +0.33% 135.24 kB 135.69 kB +0.36% 43.54 kB 43.69 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.production.min.js +0.32% 91.71 kB 92.01 kB +0.34% 28.20 kB 28.30 kB
oss-stable/react-reconciler/cjs/react-reconciler.production.min.js +0.32% 91.71 kB 92.01 kB +0.34% 28.20 kB 28.30 kB
oss-stable-semver/react-dom/umd/react-dom.profiling.min.js +0.32% 139.06 kB 139.51 kB +0.32% 45.02 kB 45.17 kB
oss-stable/react-dom/umd/react-dom.profiling.min.js +0.32% 139.06 kB 139.51 kB +0.32% 45.02 kB 45.17 kB
oss-stable-semver/react-dom/cjs/react-dom.profiling.min.js +0.32% 139.56 kB 140.01 kB +0.36% 44.43 kB 44.59 kB
oss-stable/react-dom/cjs/react-dom.profiling.min.js +0.32% 139.56 kB 140.01 kB +0.36% 44.43 kB 44.59 kB
oss-experimental/react-dom/cjs/react-dom.profiling.min.js +0.31% 144.33 kB 144.78 kB +0.30% 45.84 kB 45.98 kB
oss-experimental/react-dom/umd/react-dom.profiling.min.js +0.31% 143.74 kB 144.19 kB +0.25% 46.38 kB 46.49 kB
oss-experimental/react-reconciler/cjs/react-reconciler.production.min.js +0.31% 96.31 kB 96.61 kB +0.35% 29.47 kB 29.57 kB
facebook-www/ReactDOMTesting-prod.modern.js +0.30% 406.78 kB 407.99 kB +0.29% 76.74 kB 76.96 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.profiling.min.js +0.30% 100.78 kB 101.08 kB +0.35% 30.79 kB 30.90 kB
oss-stable/react-reconciler/cjs/react-reconciler.profiling.min.js +0.30% 100.78 kB 101.08 kB +0.35% 30.79 kB 30.90 kB
facebook-www/ReactDOM-prod.modern.js +0.29% 413.04 kB 414.25 kB +0.29% 76.50 kB 76.73 kB
facebook-www/ReactDOMForked-prod.modern.js +0.29% 413.04 kB 414.25 kB +0.29% 76.51 kB 76.73 kB
facebook-www/ReactDOMTesting-prod.classic.js +0.29% 420.10 kB 421.30 kB +0.28% 78.91 kB 79.13 kB
facebook-www/ReactDOM-prod.classic.js +0.28% 424.49 kB 425.69 kB +0.33% 78.24 kB 78.49 kB
facebook-www/ReactDOMForked-prod.classic.js +0.28% 424.49 kB 425.69 kB +0.33% 78.24 kB 78.50 kB
oss-experimental/react-reconciler/cjs/react-reconciler.profiling.min.js +0.28% 105.40 kB 105.69 kB +0.35% 32.15 kB 32.27 kB
facebook-www/ReactDOM-profiling.modern.js +0.27% 444.87 kB 446.08 kB +0.29% 81.92 kB 82.15 kB
facebook-www/ReactDOMForked-profiling.modern.js +0.27% 444.87 kB 446.08 kB +0.29% 81.92 kB 82.16 kB
facebook-www/ReactDOM-profiling.classic.js +0.26% 456.37 kB 457.57 kB +0.28% 83.69 kB 83.93 kB
facebook-www/ReactDOMForked-profiling.classic.js +0.26% 456.37 kB 457.57 kB +0.29% 83.69 kB 83.93 kB

Generated by 🚫 dangerJS against f898b8c

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Nov 22, 2021
@sebmarkbage
Copy link
Collaborator

The general principle is that we try to avoid modifying the DOM during hydration if possible because otherwise you may trigger a layout pass when nothing has changed from the original layout or unnecessarily early.

I think before the throw we still ended up inserting it as a compromise. The ideal solution would be that the client doesn’t insert empty text node and so there’s no need to hydrate one. There’s a comment in the code about that.

So the previous solution wasn’t ideal but at least they’re all inserted at once.

By doing it on the render phase, you risk triggering layout once per frame. I’d say each frame during hydration you discover another empty text node. That has the potential to really slow down hydration in bad cases.

One consideration.

@salazarm
Copy link
Contributor Author

The general principle is that we try to avoid modifying the DOM during hydration if possible because otherwise you may trigger a layout pass when nothing has changed from the original layout or unnecessarily early.

That makes sense.

I think before the throw we still ended up inserting it as a compromise.

Right, we would end up doing an insertion. So in this particular case just fallback to the old behavior: client-render the rest of the tree (and throw away the SSR content for the remaining tree)?

The ideal solution would be that the client doesn’t insert empty text node and so there’s no need to hydrate one. There’s a comment in the code about that.

I see, so we recommend people not to output empty strings at least in SSR?

@sebmarkbage
Copy link
Collaborator

I see, so we recommend people not to output empty strings at least in SSR?

No, React could do that automatically. There's no point in us inserting an empty text node. It's just an artifact of the implementation details and it makes it easier (faster) to update when text content changes.

@salazarm
Copy link
Contributor Author

Ah okay good point. I'll look into that

@salazarm salazarm closed this Nov 22, 2021
@salazarm salazarm deleted the emptyStringHydrationSimple branch February 10, 2022 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants