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

Print attributes in hydration errors #24167

Closed
wants to merge 2 commits into from
Closed

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Mar 25, 2022

The mildest version I can think of that still adds something useful to the messages. Doesn't require any extra wiring but should help when you have class names or styles. This primarily annotates the parent so it's not as helpful as it would be if we said which child didn't match. But I think it's better than status quo.

@gaearon gaearon requested review from sebmarkbage and acdlite March 25, 2022 19:26
@facebook-github-bot facebook-github-bot added the React Core Team Opened by a member of the React Core Team label Mar 25, 2022
if (value.length > 30) {
trimmedValue = value.substr(0, 30) + '...';
}
str += ' ' + attributeName + '="' + trimmedValue + '"';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This doesn't escape trimmedValue but I think that's fine. If we wanted to escape we'd need to move the escaping utility from the server folder or copypaste here.

str += ' ...';
break;
}
const attributeName = attributeNames[i];
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I could get a React name for this but seems confusing. E.g. it would still print style as a string. So I figured it should just use HTML in the message.

@sizebot
Copy link

sizebot commented Mar 25, 2022

Comparing: 3787230...c5232b2

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 = 131.32 kB 131.32 kB = 41.96 kB 41.96 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 136.38 kB 136.38 kB = 43.42 kB 43.42 kB
facebook-www/ReactDOM-prod.classic.js = 432.73 kB 432.73 kB = 79.60 kB 79.60 kB
facebook-www/ReactDOM-prod.modern.js = 417.73 kB 417.73 kB = 77.22 kB 77.22 kB
facebook-www/ReactDOMForked-prod.classic.js = 432.73 kB 432.73 kB = 79.60 kB 79.60 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against c5232b2

@@ -1244,9 +1270,9 @@ export function warnForInsertedHydratedElement(
}
didWarnInvalidHydration = true;
console.error(
'Expected server HTML to contain a matching <%s> in <%s>.',
'Expected server HTML to contain a matching <%s> in %s.',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it clear what the "in" really means here? That's the thing that I always found vague. Like are these the mismatches, or is one parent and one child?

I feel like the message you use some formatting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I'd propose changing it more significantly. Something like

There is a mismatch between the client and server render output.
The mismatch occurred inside of this parent element:

  <div class="blabla parent">

The last successfully matched child was:

  <h1 class="blabla">

After it, the server rendered:

  <h3 class="blabla">

But the client rendered:

  <h2 class="blabla">

Fix the error by making the client and the server output the same.

Thoughts?

Copy link
Collaborator

@sebmarkbage sebmarkbage Mar 25, 2022

Choose a reason for hiding this comment

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

This has the same data but is easier to parse for me at least. More visual:

There is a mismatch between the client and server render output.
The mismatch occurred inside of this element:

  <div class="blabla parent">
    <h1 class="blabla">...</h1>
-   <h3 class="blabla">  <--- server
+   <h2 class="blabla">  <--- client
    ...
  </div>

Copy link
Collaborator

Choose a reason for hiding this comment

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

The same format translates to missing and extra nodes too.

Copy link
Collaborator

@acdlite acdlite left a comment

Choose a reason for hiding this comment

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

Aside from stylistic nits/bikeshedding the overall approach is good with me

@gaearon
Copy link
Collaborator Author

gaearon commented Apr 1, 2022

#24250

@gaearon gaearon deleted the ssr-warnings branch April 1, 2022 19:58
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.

5 participants