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,20 +80,42 @@ export function PseudoHtmlDiff({ ...@@ -80,20 +80,42 @@ export function PseudoHtmlDiff({
const nestedHtmlStack: React.ReactNode[] = [] const nestedHtmlStack: React.ReactNode[] = []
let lastText = '' let lastText = ''
componentStackFrames const componentStack = componentStackFrames
.map((frame) => frame.component) .map((frame) => frame.component)
.reverse() .reverse()
.forEach((component, index, componentList) => {
// [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
}
}
}
componentStack.forEach((component, index, componentList) => {
const spaces = ' '.repeat(nestedHtmlStack.length * 2) const spaces = ' '.repeat(nestedHtmlStack.length * 2)
const prevComponent = componentList[index - 1] // const prevComponent = componentList[index - 1]
const nextComponent = componentList[index + 1] // const nextComponent = componentList[index + 1]
// When component is the server or client tag name, highlight it // When component is the server or client tag name, highlight it
const isHighlightedTag = tagNames.includes(component) const isHighlightedTag = isHtmlTagsWarning
? index === matchedIndex[0] || index === matchedIndex[1]
: tagNames.includes(component)
const isAdjacentTag = const isAdjacentTag =
isHighlightedTag || isHighlightedTag ||
tagNames.includes(prevComponent) || Math.abs(index - matchedIndex[0]) <= 1 ||
tagNames.includes(nextComponent) Math.abs(index - matchedIndex[1]) <= 1
const isLastFewFrames = const isLastFewFrames =
!isHtmlTagsWarning && index >= componentList.length - 6 !isHtmlTagsWarning && index >= componentList.length - 6
......
...@@ -421,9 +421,13 @@ describe('Error overlay for hydration errors', () => { ...@@ -421,9 +421,13 @@ describe('Error overlay for hydration errors', () => {
export default function Page() { export default function Page() {
return ( return (
<div>
<div>
<p> <p>
<div>Nested div under p tag</div> <div>Nested div under p tag</div>
</p> </p>
</div>
</div>
) )
} }
`, `,
...@@ -451,7 +455,7 @@ describe('Error overlay for hydration errors', () => { ...@@ -451,7 +455,7 @@ describe('Error overlay for hydration errors', () => {
if (isTurbopack) { if (isTurbopack) {
expect(pseudoHtml).toMatchInlineSnapshot(` expect(pseudoHtml).toMatchInlineSnapshot(`
"... "...
<Page> <div>
<p> <p>
^^^ ^^^
<div> <div>
...@@ -460,6 +464,8 @@ describe('Error overlay for hydration errors', () => { ...@@ -460,6 +464,8 @@ describe('Error overlay for hydration errors', () => {
} else { } else {
expect(pseudoHtml).toMatchInlineSnapshot(` expect(pseudoHtml).toMatchInlineSnapshot(`
"<Page> "<Page>
<div>
<div>
<p> <p>
^^^ ^^^
<div> <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