Unverified Commit 53c21881 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fix(error-overlay): matching html tag with brackets in hydration error (#63365)

### What

* The tags extracted from hydration errors could be "<div>" and "p", we
need to stripe the brackets "<>" for matching the tag name from
component stack
* Change the dimmed text color to lighter so you can see it better in
light mode

#### After vs Before

<img width="450"
src="https://github.com/vercel/next.js/assets/4800338/6caed5ac-c073-41cc-a699-eb29f3785d59">

<img width="450"
src="https://github.com/vercel/next.js/assets/4800338/926aa80f-2a49-4362-b77e-16b819955b0a">


Closes NEXT-2828
parent dc2be648
...@@ -73,7 +73,10 @@ export function PseudoHtmlDiff({ ...@@ -73,7 +73,10 @@ export function PseudoHtmlDiff({
const [isHtmlCollapsed, toggleCollapseHtml] = useState(shouldCollapse) const [isHtmlCollapsed, toggleCollapseHtml] = useState(shouldCollapse)
const htmlComponents = useMemo(() => { const htmlComponents = useMemo(() => {
const tagNames = isHtmlTagsWarning ? [firstContent, secondContent] : [] const tagNames = isHtmlTagsWarning
? // tags could have < or > in the name, so we always remove them to match
[firstContent.replace(/<|>/g, ''), secondContent.replace(/<|>/g, '')]
: []
const nestedHtmlStack: React.ReactNode[] = [] const nestedHtmlStack: React.ReactNode[] = []
let lastText = '' let lastText = ''
......
...@@ -216,6 +216,6 @@ export const styles = css` ...@@ -216,6 +216,6 @@ export const styles = css`
font-size: 0; font-size: 0;
} }
[data-nextjs-container-errors-pseudo-html--tag-adjacent='false'] { [data-nextjs-container-errors-pseudo-html--tag-adjacent='false'] {
color: var(--color-accents-3); color: var(--color-accents-1);
} }
` `
...@@ -19,10 +19,9 @@ export const getHydrationWarningType = ( ...@@ -19,10 +19,9 @@ export const getHydrationWarningType = (
const isHtmlTagsWarning = (msg: NullableText) => const isHtmlTagsWarning = (msg: NullableText) =>
Boolean(msg && htmlTagsWarnings.has(msg)) Boolean(msg && htmlTagsWarnings.has(msg))
const isTextMismatchWarning = (msg: NullableText) => const isTextMismatchWarning = (msg: NullableText) => textMismatchWarning === msg
Boolean(msg && textMismatchWarnings.has(msg))
const isTextInTagsMismatchWarning = (msg: NullableText) => const isTextInTagsMismatchWarning = (msg: NullableText) =>
Boolean(msg && textInTagsMismatchWarnings.has(msg)) Boolean(msg && textAndTagsMismatchWarnings.has(msg))
const isKnownHydrationWarning = (msg: NullableText) => const isKnownHydrationWarning = (msg: NullableText) =>
isHtmlTagsWarning(msg) || isHtmlTagsWarning(msg) ||
...@@ -37,13 +36,12 @@ const htmlTagsWarnings = new Set([ ...@@ -37,13 +36,12 @@ const htmlTagsWarnings = new Set([
'Warning: Expected server HTML to contain a matching <%s> in <%s>.%s', 'Warning: Expected server HTML to contain a matching <%s> in <%s>.%s',
'Warning: Did not expect server HTML to contain a <%s> in <%s>.%s', 'Warning: Did not expect server HTML to contain a <%s> in <%s>.%s',
]) ])
const textInTagsMismatchWarnings = new Set([ const textAndTagsMismatchWarnings = new Set([
'Warning: Expected server HTML to contain a matching text node for "%s" in <%s>.%s', 'Warning: Expected server HTML to contain a matching text node for "%s" in <%s>.%s',
'Warning: Did not expect server HTML to contain the text node "%s" in <%s>.%s', 'Warning: Did not expect server HTML to contain the text node "%s" in <%s>.%s',
]) ])
const textMismatchWarnings = new Set([ const textMismatchWarning =
'Warning: Text content did not match. Server: "%s" Client: "%s"%s', 'Warning: Text content did not match. Server: "%s" Client: "%s"%s'
])
/** /**
* Patch console.error to capture hydration errors. * Patch console.error to capture hydration errors.
......
...@@ -349,7 +349,7 @@ describe('Error overlay for hydration errors', () => { ...@@ -349,7 +349,7 @@ describe('Error overlay for hydration errors', () => {
await cleanup() await cleanup()
}) })
it('should only show one hydration error when bad nesting happened', async () => { it('should only show one hydration error when bad nesting happened - p under p', async () => {
const { cleanup, session, browser } = await sandbox( const { cleanup, session, browser } = await sandbox(
next, next,
new Map([ new Map([
...@@ -412,6 +412,69 @@ describe('Error overlay for hydration errors', () => { ...@@ -412,6 +412,69 @@ describe('Error overlay for hydration errors', () => {
await cleanup() await cleanup()
}) })
it('should only show one hydration error when bad nesting happened - div under p', async () => {
const { cleanup, session, browser } = await sandbox(
next,
new Map([
[
'app/page.js',
outdent`
'use client'
export default function Page() {
return (
<p>
<div>Nested div under p tag</div>
</p>
)
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.hasRedbox()).toBe(true)
const totalErrorCount = await browser
.elementByCss('[data-nextjs-dialog-header-total-count]')
.text()
expect(totalErrorCount).toBe('1')
const description = await session.getRedboxDescription()
expect(description).toContain(
'Error: Hydration failed because the initial UI does not match what was rendered on the server.'
)
const warning = await session.getRedboxDescriptionWarning()
expect(warning).toContain(
'In HTML, <div> cannot be a descendant of <p>.\nThis will cause a hydration error.'
)
const pseudoHtml = await session.getRedboxComponentStack()
// Turbopack currently has longer component stack trace
if (isTurbopack) {
expect(pseudoHtml).toMatchInlineSnapshot(`
"...
<Page>
<p>
^^^
<div>
^^^^^"
`)
} else {
expect(pseudoHtml).toMatchInlineSnapshot(`
"<Page>
<p>
^^^
<div>
^^^^^"
`)
}
await cleanup()
})
it('should show the highlighted bad nesting html snippet when bad nesting happened', async () => { it('should show the highlighted bad nesting html snippet when bad nesting happened', async () => {
const { cleanup, session } = await sandbox( const { cleanup, session } = await sandbox(
next, next,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment