Unverified Commit 4d4b45ec authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fix dev overlay pseudo html collapsing (#62728)

Dev overlay should show the full stack trace after clicking the
uncollase button to display the full component stack

Closes NEXT-2658
parent 80340422
...@@ -2,9 +2,6 @@ import { useMemo, Fragment, useState } from 'react' ...@@ -2,9 +2,6 @@ import { useMemo, Fragment, useState } from 'react'
import type { ComponentStackFrame } from '../../helpers/parse-component-stack' import type { ComponentStackFrame } from '../../helpers/parse-component-stack'
import { CollapseIcon } from '../../icons/CollapseIcon' import { CollapseIcon } from '../../icons/CollapseIcon'
// In total it will display 6 rows.
const MAX_NON_COLLAPSED_FRAMES = 6
/** /**
* *
* Format component stack into pseudo HTML * Format component stack into pseudo HTML
...@@ -51,6 +48,8 @@ export function PseudoHtmlDiff({ ...@@ -51,6 +48,8 @@ export function PseudoHtmlDiff({
hydrationMismatchType: 'tag' | 'text' hydrationMismatchType: 'tag' | 'text'
} & React.HTMLAttributes<HTMLPreElement>) { } & React.HTMLAttributes<HTMLPreElement>) {
const isHtmlTagsWarning = hydrationMismatchType === 'tag' const isHtmlTagsWarning = hydrationMismatchType === 'tag'
// For text mismatch, mismatched text will take 2 rows, so we display 4 rows of component stack
const MAX_NON_COLLAPSED_FRAMES = isHtmlTagsWarning ? 6 : 4
const shouldCollapse = componentStackFrames.length > MAX_NON_COLLAPSED_FRAMES const shouldCollapse = componentStackFrames.length > MAX_NON_COLLAPSED_FRAMES
const [isHtmlCollapsed, toggleCollapseHtml] = useState(shouldCollapse) const [isHtmlCollapsed, toggleCollapseHtml] = useState(shouldCollapse)
...@@ -77,16 +76,10 @@ export function PseudoHtmlDiff({ ...@@ -77,16 +76,10 @@ export function PseudoHtmlDiff({
const isLastFewFrames = const isLastFewFrames =
!isHtmlTagsWarning && index >= componentList.length - 6 !isHtmlTagsWarning && index >= componentList.length - 6
if (
nestedHtmlStack.length >= MAX_NON_COLLAPSED_FRAMES &&
isHtmlCollapsed
) {
return
}
if ((isHtmlTagsWarning && isRelatedTag) || isLastFewFrames) { if ((isHtmlTagsWarning && isRelatedTag) || isLastFewFrames) {
const codeLine = ( const codeLine = (
<span> <span>
<span>{spaces}</span> {spaces}
<span <span
{...(isHighlightedTag {...(isHighlightedTag
? { ? {
...@@ -112,7 +105,14 @@ export function PseudoHtmlDiff({ ...@@ -112,7 +105,14 @@ export function PseudoHtmlDiff({
) )
nestedHtmlStack.push(wrappedCodeLine) nestedHtmlStack.push(wrappedCodeLine)
} else { } else {
if ((isHtmlTagsWarning && !isHtmlCollapsed) || isLastFewFrames) { if (
nestedHtmlStack.length >= MAX_NON_COLLAPSED_FRAMES &&
isHtmlCollapsed
) {
return
}
if (!isHtmlCollapsed || isLastFewFrames) {
nestedHtmlStack.push( nestedHtmlStack.push(
<span key={nestedHtmlStack.length}> <span key={nestedHtmlStack.length}>
{spaces} {spaces}
...@@ -154,6 +154,7 @@ export function PseudoHtmlDiff({ ...@@ -154,6 +154,7 @@ export function PseudoHtmlDiff({
serverContent, serverContent,
isHtmlTagsWarning, isHtmlTagsWarning,
hydrationMismatchType, hydrationMismatchType,
MAX_NON_COLLAPSED_FRAMES,
]) ])
return ( return (
......
...@@ -27,13 +27,40 @@ describe('Component Stack in error overlay', () => { ...@@ -27,13 +27,40 @@ describe('Component Stack in error overlay', () => {
<main> <main>
<Component> <Component>
<div> <div>
<p>
"server" "server"
"client"" "client""
`) `)
await session.toggleComponentStack() await session.toggleCollapseComponentStack()
expect(await session.getRedboxComponentStack()).toMatchInlineSnapshot(` expect(await session.getRedboxComponentStack()).toMatchInlineSnapshot(`
"<InnerLayoutRouter> "<Root>
<ServerRoot>
<AppRouter>
<ErrorBoundary>
<ErrorBoundaryHandler>
<Router>
<HotReload>
<ReactDevOverlay>
<DevRootNotFoundBoundary>
<NotFoundBoundary>
<NotFoundErrorBoundary>
<RedirectBoundary>
<RedirectErrorBoundary>
<RootLayout>
<html>
<body>
<OuterLayoutRouter>
<RenderFromTemplateContext>
<ScrollAndFocusHandler>
<InnerScrollAndFocusHandler>
<ErrorBoundary>
<LoadingBoundary>
<NotFoundBoundary>
<NotFoundErrorBoundary>
<RedirectBoundary>
<RedirectErrorBoundary>
<InnerLayoutRouter>
<Mismatch> <Mismatch>
<main> <main>
<Component> <Component>
......
...@@ -58,6 +58,7 @@ describe('Error overlay for hydration errors', () => { ...@@ -58,6 +58,7 @@ describe('Error overlay for hydration errors', () => {
<InnerLayoutRouter> <InnerLayoutRouter>
<Mismatch> <Mismatch>
<div> <div>
<main>
"server" "server"
"client"" "client""
`) `)
...@@ -430,7 +431,9 @@ describe('Error overlay for hydration errors', () => { ...@@ -430,7 +431,9 @@ describe('Error overlay for hydration errors', () => {
^^^ ^^^
<span> <span>
... ...
<span>" <span>
<p>
^^^"
`) `)
} else { } else {
expect(pseudoHtml).toMatchInlineSnapshot(` expect(pseudoHtml).toMatchInlineSnapshot(`
...@@ -447,4 +450,121 @@ describe('Error overlay for hydration errors', () => { ...@@ -447,4 +450,121 @@ describe('Error overlay for hydration errors', () => {
await cleanup() await cleanup()
}) })
it('should collapse and uncollapse properly when there are many frames', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/page.js',
outdent`
'use client'
const isServer = typeof window === 'undefined'
function Mismatch() {
return (
<p>
<span>
hello {isServer ? 'server' : 'client'}
</span>
</p>
)
}
export default function Page() {
return (
<div>
<div>
<div>
<div>
<Mismatch />
</div>
</div>
</div>
</div>
)
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.hasRedbox()).toBe(true)
const pseudoHtml = await session.getRedboxComponentStack()
expect(pseudoHtml).toMatchInlineSnapshot(`
"...
<div>
<div>
<div>
<Mismatch>
<p>
<span>
"server"
"client""
`)
await session.toggleCollapseComponentStack()
const fullPseudoHtml = await session.getRedboxComponentStack()
if (isTurbopack) {
expect(fullPseudoHtml).toMatchInlineSnapshot(`
"<Root>
<ServerRoot>
<AppRouter>
<ErrorBoundary>
<ErrorBoundaryHandler>
<Router>
<HotReload>
<ReactDevOverlay>
<DevRootNotFoundBoundary>
<NotFoundBoundary>
<NotFoundErrorBoundary>
<RedirectBoundary>
<RedirectErrorBoundary>
<RootLayout>
<html>
<body>
<OuterLayoutRouter>
<RenderFromTemplateContext>
<ScrollAndFocusHandler>
<InnerScrollAndFocusHandler>
<ErrorBoundary>
<LoadingBoundary>
<NotFoundBoundary>
<NotFoundErrorBoundary>
<RedirectBoundary>
<RedirectErrorBoundary>
<InnerLayoutRouter>
<Page>
<div>
<div>
<div>
<div>
<Mismatch>
<p>
<span>
"server"
"client""
`)
} else {
expect(fullPseudoHtml).toMatchInlineSnapshot(`
"<Page>
<div>
<div>
<div>
<div>
<Mismatch>
<p>
<span>
"server"
"client""
`)
}
await cleanup()
})
}) })
...@@ -22,6 +22,7 @@ createNextDescribe( ...@@ -22,6 +22,7 @@ createNextDescribe(
<main> <main>
<Component> <Component>
<div> <div>
<p>
"server" "server"
"client"" "client""
`) `)
......
...@@ -9,7 +9,7 @@ import { ...@@ -9,7 +9,7 @@ import {
waitFor, waitFor,
waitForAndOpenRuntimeError, waitForAndOpenRuntimeError,
getRedboxDescriptionWarning, getRedboxDescriptionWarning,
toggleComponentStack, toggleCollapseComponentStack,
} from './next-test-utils' } from './next-test-utils'
import webdriver from './next-webdriver' import webdriver from './next-webdriver'
import { NextInstance } from './next-modes/base' import { NextInstance } from './next-modes/base'
...@@ -139,8 +139,8 @@ export async function sandbox( ...@@ -139,8 +139,8 @@ export async function sandbox(
async getRedboxComponentStack() { async getRedboxComponentStack() {
return getRedboxComponentStack(browser) return getRedboxComponentStack(browser)
}, },
async toggleComponentStack() { async toggleCollapseComponentStack() {
return toggleComponentStack(browser) return toggleCollapseComponentStack(browser)
}, },
async getVersionCheckerText() { async getVersionCheckerText() {
return getVersionCheckerText(browser) return getVersionCheckerText(browser)
......
...@@ -1068,7 +1068,7 @@ export async function getRedboxComponentStack( ...@@ -1068,7 +1068,7 @@ export async function getRedboxComponentStack(
return componentStackFrameTexts.join('\n').trim() return componentStackFrameTexts.join('\n').trim()
} }
export async function toggleComponentStack( export async function toggleCollapseComponentStack(
browser: BrowserInterface browser: BrowserInterface
): Promise<void> { ): Promise<void> {
await browser await browser
......
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