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

Show HTML Diff in Dev Error Overlay for Hydration Errors #37847

Closed
karlhorky opened this issue Jun 20, 2022 · 10 comments
Closed

Show HTML Diff in Dev Error Overlay for Hydration Errors #37847

karlhorky opened this issue Jun 20, 2022 · 10 comments
Labels

Comments

@karlhorky
Copy link
Contributor

karlhorky commented Jun 20, 2022

Describe the feature you'd like to request

Hi there 👋 As always, thanks for the continuing effort on Next.js!! 🙌

The messaging for hydration errors in the dev overlay in Next.js leave something to be desired.

Hydration errors can happen for a variety of reasons, but looking at an example of invalid DOM nesting (based on Triggering validateDOMNesting causes Hydration failed (#36765)), the error is very generic and not very helpful (especially for beginners).

Here's a StackBlitz: https://stackblitz.com/edit/nextjs-iqm44f?file=pages%2Findex.js

Screen Shot 2022-06-20 at 16 23 37

Paging to the 2nd and 3rd errors using the arrows at the top left, the messages and traces are also equally generic:

Screen Shot 2022-06-20 at 16 24 09

Screen Shot 2022-06-20 at 16 25 29

If you open the DevTools console, you will find some more useful information:

Screen Shot 2022-06-20 at 16 26 29

Screen Shot 2022-06-20 at 16 26 51

However, this is hidden away in the DevTools, and mixed in with other generic or misleading errors:

Screen Shot 2022-06-20 at 16 26 47

Describe the solution you'd like

Error messages for any hydration errors in the dev overlay should be:

A) Useful
B) Immediately actionable (without further research)

For me, this would indicate:

  1. Displaying the expected and actual html
  2. Displaying the component name and filename where the error occured (maybe also a clickable link to the file)
  3. Displaying a "diff"-type design of the expected and actual html, formatted and syntax highlighted, like the one below
- server
+ client

-<p>
+<p />
  <p>initial</p>
-</p>
+<p />

cc @gaearon

Describe alternatives you've considered

Creating an ESLint rule to catch the issue before the error ever appears in the browser: jsx-eslint/eslint-plugin-react#3310

@gaearon
Copy link
Contributor

gaearon commented Jun 20, 2022

I wonder if this is a good time to consider showing console.error from React in the box. Because conceptually they are errors and typically contain the actual cause before something throws.

@karlhorky karlhorky changed the title Improve Dev Error Overlay for Hydration Errors Show HTML Diff in Dev Error Overlay for Hydration Errors Jan 28, 2023
@karlhorky
Copy link
Contributor Author

karlhorky commented Nov 28, 2023

Nice, TIL about the WIP PRs to React from @gaearon and @himself65

@karlhorky
Copy link
Contributor Author

karlhorky commented Jan 30, 2024

Alternative: @samijaber created @builder.io/react-hydration-overlay, which could be pulled into Next.js core

@himself65
Copy link
Contributor

This is cool

Alternative: @samijaber created @builder.io/react-hydration-overlay, which could be pulled into Next.js core

@himself65
Copy link
Contributor

It captures two timestamp html and uses DiffViewer.

https://github.com/BuilderIO/hydration-overlay/blob/661aa0e17a50777fe9d7222fe0759d84798b1c3e/packages/lib/src/Overlay.tsx#L8

@huozhi
Copy link
Member

huozhi commented Apr 11, 2024

We landed a hydration diff in 14.2.0, please checkout the blog post! I'll close this issue for now!

Related PRs: #62590, #62684, #62728, #63342 #62993, #63288, #63365, #63403

@huozhi huozhi closed this as completed Apr 11, 2024
@karlhorky
Copy link
Contributor Author

Amazing, thanks @huozhi 🎉 Look forward to using it. This will be a huge help for learners / beginners too.

@rtman
Copy link

rtman commented Apr 25, 2024

@huozhi Ive just tried this and am not seeing any diff or component stack. Is this only for app router (we are using pages). Is there something that I need to do to enable this?

For reference I'm using 14.2.3
Screenshot 2024-04-25 at 3 28 42 PM

@huozhi
Copy link
Member

huozhi commented Apr 26, 2024

@rtman can you file a new issue with reproduction for this? Thanks

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants