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

Add Diffs to Hydration Warnings #28512

Merged
merged 8 commits into from
Mar 27, 2024
Merged

Conversation

sebmarkbage
Copy link
Collaborator

@sebmarkbage sebmarkbage commented Mar 7, 2024

Stacked on #28502.

This builds on the mechanism in #28502 by adding a diff of everything we've collected so far to the thrown error or logged error.

This isn't actually a longest common subsequence diff. This means that there are certain cases that can appear confusing such as a node being added/removed when it really would've appeared later in the list. In fact once a node mismatches, we abort rendering so we don't have the context of what would've been rendered. It's not quite right to use the result of the recovery render because it can use client-only code paths using useSyncExternalStore which would yield false differences. That's why diffing the HTML isn't quite right.

I also present abstract components in the stack, these are presented with the client props and no diff since we don't have the props that were on the server. The lack of difference might be confusing but it's useful for context.

The main thing that's data new here is that we're adding some siblings and props for context.

Examples in the snapshot commit.

@react-sizebot
Copy link

react-sizebot commented Mar 7, 2024

Comparing: f7aa5e0...2f2eb4d

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 = 175.85 kB 175.85 kB = 54.68 kB 54.67 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 172.33 kB 172.33 kB = 53.81 kB 53.81 kB
facebook-www/ReactDOM-prod.classic.js = 589.61 kB 589.61 kB = 103.77 kB 103.77 kB
facebook-www/ReactDOM-prod.modern.js = 573.13 kB 573.13 kB = 100.81 kB 100.81 kB
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-dev.js +2.13% 924.30 kB 944.01 kB +2.27% 183.34 kB 187.50 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.development.js +2.09% 840.33 kB 857.93 kB +2.26% 176.43 kB 180.41 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.development.js +2.09% 802.37 kB 819.11 kB +2.27% 174.37 kB 178.33 kB
facebook-www/ReactTestRenderer-dev.modern.js +2.09% 945.09 kB 964.80 kB +2.24% 187.58 kB 191.79 kB
facebook-www/ReactTestRenderer-dev.classic.js +2.09% 945.09 kB 964.80 kB +2.24% 187.58 kB 191.79 kB
react-native/implementations/ReactFabric-dev.js +2.07% 950.64 kB 970.35 kB +2.15% 190.57 kB 194.67 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.development.js +2.06% 855.10 kB 872.70 kB +2.19% 179.37 kB 183.30 kB
oss-stable/react-test-renderer/umd/react-test-renderer.development.js +2.06% 855.13 kB 872.72 kB +2.19% 179.40 kB 183.33 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.development.js +2.05% 816.55 kB 833.29 kB +2.22% 177.38 kB 181.31 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.development.js +2.05% 816.57 kB 833.32 kB +2.22% 177.41 kB 181.34 kB
react-native/implementations/ReactNativeRenderer-dev.js +2.04% 965.54 kB 985.25 kB +2.14% 194.22 kB 198.37 kB
oss-experimental/react-art/cjs/react-art.development.js +2.03% 825.84 kB 842.59 kB +2.22% 178.21 kB 182.17 kB
oss-stable-semver/react-art/cjs/react-art.development.js +2.01% 832.22 kB 848.97 kB +2.17% 180.06 kB 183.96 kB
oss-stable/react-art/cjs/react-art.development.js +2.01% 832.25 kB 848.99 kB +2.17% 180.08 kB 183.99 kB
test_utils/ReactAllWarnings.js Deleted 65.20 kB 0.00 kB Deleted 16.32 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-dev.js +2.13% 924.30 kB 944.01 kB +2.27% 183.34 kB 187.50 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.development.js +2.09% 840.33 kB 857.93 kB +2.26% 176.43 kB 180.41 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.development.js +2.09% 802.37 kB 819.11 kB +2.27% 174.37 kB 178.33 kB
facebook-www/ReactTestRenderer-dev.modern.js +2.09% 945.09 kB 964.80 kB +2.24% 187.58 kB 191.79 kB
facebook-www/ReactTestRenderer-dev.classic.js +2.09% 945.09 kB 964.80 kB +2.24% 187.58 kB 191.79 kB
react-native/implementations/ReactFabric-dev.js +2.07% 950.64 kB 970.35 kB +2.15% 190.57 kB 194.67 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.development.js +2.06% 855.10 kB 872.70 kB +2.19% 179.37 kB 183.30 kB
oss-stable/react-test-renderer/umd/react-test-renderer.development.js +2.06% 855.13 kB 872.72 kB +2.19% 179.40 kB 183.33 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.development.js +2.05% 816.55 kB 833.29 kB +2.22% 177.38 kB 181.31 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.development.js +2.05% 816.57 kB 833.32 kB +2.22% 177.41 kB 181.34 kB
react-native/implementations/ReactNativeRenderer-dev.js +2.04% 965.54 kB 985.25 kB +2.14% 194.22 kB 198.37 kB
oss-experimental/react-art/cjs/react-art.development.js +2.03% 825.84 kB 842.59 kB +2.22% 178.21 kB 182.17 kB
oss-stable-semver/react-art/cjs/react-art.development.js +2.01% 832.22 kB 848.97 kB +2.17% 180.06 kB 183.96 kB
oss-stable/react-art/cjs/react-art.development.js +2.01% 832.25 kB 848.99 kB +2.17% 180.08 kB 183.99 kB
oss-experimental/react-reconciler/cjs/react-reconciler.development.js +1.95% 919.97 kB 937.91 kB +2.03% 197.18 kB 201.18 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.development.js +1.94% 923.76 kB 941.69 kB +2.01% 197.99 kB 201.97 kB
oss-stable/react-reconciler/cjs/react-reconciler.development.js +1.94% 923.78 kB 941.72 kB +2.01% 198.02 kB 202.00 kB
facebook-www/ReactART-dev.modern.js +1.88% 1,048.22 kB 1,067.93 kB +2.02% 204.93 kB 209.07 kB
oss-experimental/react-art/umd/react-art.development.js +1.87% 941.71 kB 959.30 kB +1.97% 197.53 kB 201.43 kB
facebook-www/ReactART-dev.classic.js +1.86% 1,060.42 kB 1,080.13 kB +2.00% 207.37 kB 211.50 kB
oss-stable-semver/react-art/umd/react-art.development.js +1.86% 948.24 kB 965.83 kB +1.92% 199.39 kB 203.22 kB
oss-stable/react-art/umd/react-art.development.js +1.86% 948.26 kB 965.86 kB +1.92% 199.41 kB 203.24 kB
react-native/implementations/ReactFabric-dev.fb.js +1.83% 1,074.98 kB 1,094.69 kB +1.94% 213.22 kB 217.36 kB
react-native/implementations/ReactNativeRenderer-dev.fb.js +1.81% 1,089.60 kB 1,109.31 kB +1.91% 217.07 kB 221.23 kB
oss-experimental/react-dom/umd/react-dom.development.js +1.40% 1,369.98 kB 1,389.14 kB +1.40% 291.82 kB 295.90 kB
oss-experimental/react-dom/cjs/react-dom.development.js +1.40% 1,306.91 kB 1,325.14 kB +1.40% 288.87 kB 292.91 kB
oss-stable-semver/react-dom/umd/react-dom.development.js +1.39% 1,379.08 kB 1,398.24 kB +1.41% 293.35 kB 297.48 kB
oss-stable/react-dom/umd/react-dom.development.js +1.39% 1,379.10 kB 1,398.26 kB +1.41% 293.38 kB 297.50 kB
oss-stable-semver/react-dom/cjs/react-dom.development.js +1.39% 1,315.75 kB 1,333.99 kB +1.40% 290.39 kB 294.46 kB
oss-stable/react-dom/cjs/react-dom.development.js +1.39% 1,315.78 kB 1,334.01 kB +1.40% 290.43 kB 294.49 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.development.js +1.38% 1,324.70 kB 1,342.94 kB +1.39% 293.09 kB 297.17 kB
facebook-www/ReactDOM-dev.modern.js +1.32% 1,625.69 kB 1,647.19 kB +1.35% 323.57 kB 327.92 kB
facebook-www/ReactDOMTesting-dev.modern.js +1.31% 1,647.16 kB 1,668.65 kB +1.34% 328.10 kB 332.50 kB
facebook-www/ReactDOM-dev.classic.js +1.30% 1,655.54 kB 1,677.04 kB +1.31% 328.96 kB 333.27 kB
facebook-www/ReactDOMTesting-dev.classic.js +1.28% 1,677.06 kB 1,698.56 kB +1.29% 333.51 kB 337.81 kB
test_utils/ReactAllWarnings.js Deleted 65.20 kB 0.00 kB Deleted 16.32 kB 0.00 kB

Generated by 🚫 dangerJS against 2f2eb4d

<div className="parent">
<main className="child">
+ client
- server
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason + added lines are printed before - removed lines? Usually in diff visualizations, removed lines are printed first.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@sompylasar nice to bump into you here haha, it's been a while since you were adding this...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There’s an awkward scenario that happens at the end where if we have more client siblings we don’t necessarily know if they would’ve matched so we cut off next siblings with “…”. However if there are extra server nodes at the tail then we do list them as removed next siblings which puts them at the end. Sometimes a swap looks like this scenario so I just followed that ordering to avoid sometimes flipping it.

I don’t think it’s necessarily inherent though. Could probably be changed.

'{"text-decoration":"none","color":"black","height":"10px"}' +
' Client: ' +
'{"textDecoration":"none","color":"black","height":"10px"}', // note that this is no difference
"A tree hydrated but some attributes of the server rendered HTML didn't match the client properties.",
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe drop "rendered"? easy to read it as a verb and then the sentence structure breaks

@gaearon
Copy link
Collaborator

gaearon commented Mar 7, 2024

I'm sure I could just read the tests but — what's the PROD situation? Do you get a diff in PROD or is it less detailed?

@sebmarkbage
Copy link
Collaborator Author

You get nothing in prod. Just minimized error with the error code linking to a page with this message but no context.

However, if you're logging it yourself you do have access to component stacks in onRecoverableError but it's up to a production logging tool to format that and it won't have access to props etc.

Comment on lines +398 to +390
+ style={{opacity:1}}
- style={{opacity:"0"}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this imply to users that part of the problem is one is a string and the other is a number? I wonder if we should try to parse the html attribute as a number if the hydration prop is also a number. It'd have to be clever like

if (typeof prop === 'number' && parseInt(attr).toString() === attr) {
  return parseInt(attr)
} else {
  return attr
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

unrelated, but maybe worth adding a space for readability? opacity: 1

Copy link
Collaborator Author

@sebmarkbage sebmarkbage Mar 7, 2024

Choose a reason for hiding this comment

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

Yes but I think you'll find that this is not the only place we could be clever. The more important one might be to find equivalences of values. E.g. if something else differs, we'll generate a diff and the diff will itself think that both 0 and "0" is different and it'll show up as a diff. Most of the time it's not as easy that though because most of the time you can't have a unitless number. So for example "1px" is equivalent to 1. It'll also ignore any style prop that isn't parsed since we read back the styles using the browser. So a browser specific one might show up as diff if something else in the stylesheet differences.

I wouldn't over rotate on the unit tests since they're just the ones that used to be here and all the other ones don't use snapshots that test this. There's more interesting cases to be found in production code.

I ended up not including spaces for these cases because the goal is to keep it compact enough to fit on one line. Especially in the case where these are formatted on contextual nodes because I always enforce it to take up at most one line so a few extra spaces can mean that a prop gets dropped instead. Don't really want to do a constraint solving where it tries to fit it.

Comment on lines +398 to +390
+ style={{opacity:1}}
- style={{opacity:"0"}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

unrelated, but maybe worth adding a space for readability? opacity: 1

This assumes that the description returns the string "Suspense" and that
any other hydratable node won't do that.
This collapses parent nodes of larger trees unless they're a fork, has
diff information or is close to a leaf of the tree that has diff info.
Otherwise we won't have diffs of parent props when a child later throws.
@sebmarkbage sebmarkbage merged commit 2ec2aae into facebook:main Mar 27, 2024
2 of 21 checks passed
github-actions bot pushed a commit that referenced this pull request Mar 27, 2024
Stacked on #28502.

This builds on the mechanism in #28502 by adding a diff of everything
we've collected so far to the thrown error or logged error.

This isn't actually a longest common subsequence diff. This means that
there are certain cases that can appear confusing such as a node being
added/removed when it really would've appeared later in the list. In
fact once a node mismatches, we abort rendering so we don't have the
context of what would've been rendered. It's not quite right to use the
result of the recovery render because it can use client-only code paths
using useSyncExternalStore which would yield false differences. That's
why diffing the HTML isn't quite right.

I also present abstract components in the stack, these are presented
with the client props and no diff since we don't have the props that
were on the server. The lack of difference might be confusing but it's
useful for context.

The main thing that's data new here is that we're adding some siblings
and props for context.

Examples in the [snapshot
commit](e14532f).

DiffTrain build for [2ec2aae](2ec2aae)
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
Stacked on facebook#28502.

This builds on the mechanism in facebook#28502 by adding a diff of everything
we've collected so far to the thrown error or logged error.

This isn't actually a longest common subsequence diff. This means that
there are certain cases that can appear confusing such as a node being
added/removed when it really would've appeared later in the list. In
fact once a node mismatches, we abort rendering so we don't have the
context of what would've been rendered. It's not quite right to use the
result of the recovery render because it can use client-only code paths
using useSyncExternalStore which would yield false differences. That's
why diffing the HTML isn't quite right.

I also present abstract components in the stack, these are presented
with the client props and no diff since we don't have the props that
were on the server. The lack of difference might be confusing but it's
useful for context.

The main thing that's data new here is that we're adding some siblings
and props for context.

Examples in the [snapshot
commit](facebook@e14532f).
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
Stacked on #28502.

This builds on the mechanism in #28502 by adding a diff of everything
we've collected so far to the thrown error or logged error.

This isn't actually a longest common subsequence diff. This means that
there are certain cases that can appear confusing such as a node being
added/removed when it really would've appeared later in the list. In
fact once a node mismatches, we abort rendering so we don't have the
context of what would've been rendered. It's not quite right to use the
result of the recovery render because it can use client-only code paths
using useSyncExternalStore which would yield false differences. That's
why diffing the HTML isn't quite right.

I also present abstract components in the stack, these are presented
with the client props and no diff since we don't have the props that
were on the server. The lack of difference might be confusing but it's
useful for context.

The main thing that's data new here is that we're adding some siblings
and props for context.

Examples in the [snapshot
commit](e14532f).

DiffTrain build for commit 2ec2aae.
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 React 19
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants