Unverified Commit 530382ae authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix hydration error higlight when tag matched multi times (#64133)

### What

We introduced a new algorithm to find the matched hydration error tags,
when there're two tags indicating children and parent as bad descendence
relationship. Now we search for the child first from the last index, and
then from found child to search its above parent. This way we can find
the two related tags in O(1), since they could be not directly nested.

### Why

When a hydration error occurred, such as bad nesting `div` under `p`,
one of the tag `div` is matched multiple times in the component stack,
it shouldn't be highlighted multiple times. This PR fixes the bad
matching about multiple nested tags, e.g. when there're many `div` tag
and `div` is one of the bad tag, only the one directly under p should be
highlited.

Case
```
Page > div > div > p > div
```

Current Result: all `div` and `p` get highlighted, `[]` represents
highlighted.
```
Page > [div] > [div] > [p] > [div]
```

Expected: only the related 2 tags are highlighted, `[]` represents
highlighted.
```
Page > div > div > [p] > [div]
```

Thanks @JohnPhamous for reporting the issue


Closes NEXT-3022
parent ac7607f9
......@@ -80,85 +80,107 @@ export function PseudoHtmlDiff({
const nestedHtmlStack: React.ReactNode[] = []
let lastText = ''
componentStackFrames
const componentStack = componentStackFrames
.map((frame) => frame.component)
.reverse()
.forEach((component, index, componentList) => {
const spaces = ' '.repeat(nestedHtmlStack.length * 2)
const prevComponent = componentList[index - 1]
const nextComponent = componentList[index + 1]
// When component is the server or client tag name, highlight it
const isHighlightedTag = tagNames.includes(component)
const isAdjacentTag =
isHighlightedTag ||
tagNames.includes(prevComponent) ||
tagNames.includes(nextComponent)
// [child index, parent index]
const matchedIndex = [-1, -1]
if (isHtmlTagsWarning) {
// Reverse search for the child tag
for (let i = componentStack.length - 1; i >= 0; i--) {
if (componentStack[i] === tagNames[0]) {
matchedIndex[0] = i
break
}
}
// Start searching parent tag from child tag above
for (let i = matchedIndex[0] - 1; i >= 0; i--) {
if (componentStack[i] === tagNames[1]) {
matchedIndex[1] = i
break
}
}
}
const isLastFewFrames =
!isHtmlTagsWarning && index >= componentList.length - 6
componentStack.forEach((component, index, componentList) => {
const spaces = ' '.repeat(nestedHtmlStack.length * 2)
// const prevComponent = componentList[index - 1]
// const nextComponent = componentList[index + 1]
// When component is the server or client tag name, highlight it
const adjProps = getAdjacentProps(isAdjacentTag)
const isHighlightedTag = isHtmlTagsWarning
? index === matchedIndex[0] || index === matchedIndex[1]
: tagNames.includes(component)
const isAdjacentTag =
isHighlightedTag ||
Math.abs(index - matchedIndex[0]) <= 1 ||
Math.abs(index - matchedIndex[1]) <= 1
if ((isHtmlTagsWarning && isAdjacentTag) || isLastFewFrames) {
const codeLine = (
<span>
{spaces}
<span
{...adjProps}
{...{
...(isHighlightedTag
? {
'data-nextjs-container-errors-pseudo-html--tag-error':
true,
}
: undefined),
}}
>
{`<${component}>\n`}
const isLastFewFrames =
!isHtmlTagsWarning && index >= componentList.length - 6
const adjProps = getAdjacentProps(isAdjacentTag)
if ((isHtmlTagsWarning && isAdjacentTag) || isLastFewFrames) {
const codeLine = (
<span>
{spaces}
<span
{...adjProps}
{...{
...(isHighlightedTag
? {
'data-nextjs-container-errors-pseudo-html--tag-error':
true,
}
: undefined),
}}
>
{`<${component}>\n`}
</span>
</span>
)
lastText = component
const wrappedCodeLine = (
<Fragment key={nestedHtmlStack.length}>
{codeLine}
{/* Add ^^^^ to the target tags used for snapshots but not displayed for users */}
{isHighlightedTag && (
<span data-nextjs-container-errors-pseudo-html--hint>
{spaces + '^'.repeat(component.length + 2) + '\n'}
</span>
)}
</Fragment>
)
nestedHtmlStack.push(wrappedCodeLine)
} else {
if (
nestedHtmlStack.length >= MAX_NON_COLLAPSED_FRAMES &&
isHtmlCollapsed
) {
return
}
if (!isHtmlCollapsed || isLastFewFrames) {
nestedHtmlStack.push(
<span {...adjProps} key={nestedHtmlStack.length}>
{spaces}
{'<' + component + '>\n'}
</span>
)
lastText = component
const wrappedCodeLine = (
<Fragment key={nestedHtmlStack.length}>
{codeLine}
{/* Add ^^^^ to the target tags used for snapshots but not displayed for users */}
{isHighlightedTag && (
<span data-nextjs-container-errors-pseudo-html--hint>
{spaces + '^'.repeat(component.length + 2) + '\n'}
</span>
)}
</Fragment>
} else if (isHtmlCollapsed && lastText !== '...') {
lastText = '...'
nestedHtmlStack.push(
<span {...adjProps} key={nestedHtmlStack.length}>
{spaces}
{'...\n'}
</span>
)
nestedHtmlStack.push(wrappedCodeLine)
} else {
if (
nestedHtmlStack.length >= MAX_NON_COLLAPSED_FRAMES &&
isHtmlCollapsed
) {
return
}
if (!isHtmlCollapsed || isLastFewFrames) {
nestedHtmlStack.push(
<span {...adjProps} key={nestedHtmlStack.length}>
{spaces}
{'<' + component + '>\n'}
</span>
)
} else if (isHtmlCollapsed && lastText !== '...') {
lastText = '...'
nestedHtmlStack.push(
<span {...adjProps} key={nestedHtmlStack.length}>
{spaces}
{'...\n'}
</span>
)
}
}
})
}
})
// Hydration mismatch: text or text-tag
if (!isHtmlTagsWarning) {
......
......@@ -251,16 +251,16 @@ describe('Error overlay for hydration errors', () => {
if (isTurbopack) {
expect(pseudoHtml).toMatchInlineSnapshot(`
"...
<NotFoundErrorBoundary>
<RedirectBoundary>
<RedirectErrorBoundary>
<InnerLayoutRouter>
<Mismatch>
<div>
<div>
"only""
`)
"...
<NotFoundErrorBoundary>
<RedirectBoundary>
<RedirectErrorBoundary>
<InnerLayoutRouter>
<Mismatch>
<div>
<div>
"only""
`)
} else {
expect(pseudoHtml).toMatchInlineSnapshot(`
"<Mismatch>
......@@ -421,9 +421,13 @@ describe('Error overlay for hydration errors', () => {
export default function Page() {
return (
<p>
<div>Nested div under p tag</div>
</p>
<div>
<div>
<p>
<div>Nested div under p tag</div>
</p>
</div>
</div>
)
}
`,
......@@ -451,7 +455,7 @@ describe('Error overlay for hydration errors', () => {
if (isTurbopack) {
expect(pseudoHtml).toMatchInlineSnapshot(`
"...
<Page>
<div>
<p>
^^^
<div>
......@@ -460,10 +464,12 @@ describe('Error overlay for hydration errors', () => {
} else {
expect(pseudoHtml).toMatchInlineSnapshot(`
"<Page>
<p>
^^^
<div>
<div>
^^^^^"
<p>
^^^
<div>
^^^^^"
`)
}
......
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