Unverified Commit f5e447f0 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[dev-overlay] Remove unused hydration error related code (#77929)

Also inlined some of the indirections that we no longer need.
parent fb8376a6
import { import {
getDefaultHydrationErrorMessage, getDefaultHydrationErrorMessage,
getHydrationErrorStackInfo,
isHydrationError, isHydrationError,
testReactHydrationWarning, testReactHydrationWarning,
} from '../is-hydration-error' } from '../is-hydration-error'
import { import {
hydrationErrorState, hydrationErrorState,
getReactHydrationDiffSegments,
type HydrationErrorState, type HydrationErrorState,
} from './hydration-error-info' } from './hydration-error-info'
...@@ -19,14 +19,8 @@ export function attachHydrationErrorState(error: Error) { ...@@ -19,14 +19,8 @@ export function attachHydrationErrorState(error: Error) {
return return
} }
const reactHydrationDiffSegments = getReactHydrationDiffSegments( const { message, diff } = getHydrationErrorStackInfo(error.message)
error.message if (message) {
)
// If the reactHydrationDiffSegments exists
// and the diff (reactHydrationDiffSegments[1]) exists
// e.g. the hydration diff log error.
if (reactHydrationDiffSegments) {
const diff = reactHydrationDiffSegments[1]
parsedHydrationErrorState = { parsedHydrationErrorState = {
...((error as any).details as HydrationErrorState), ...((error as any).details as HydrationErrorState),
...hydrationErrorState, ...hydrationErrorState,
...@@ -43,7 +37,7 @@ export function attachHydrationErrorState(error: Error) { ...@@ -43,7 +37,7 @@ export function attachHydrationErrorState(error: Error) {
], ],
// When it's hydration diff log, do not show notes section. // When it's hydration diff log, do not show notes section.
// This condition is only for the 1st squashed error. // This condition is only for the 1st squashed error.
notes: isHydrationWarning ? '' : reactHydrationDiffSegments[0], notes: isHydrationWarning ? '' : message,
reactOutputComponentDiff: diff, reactOutputComponentDiff: diff,
} }
// Cache the `reactOutputComponentDiff` into hydrationErrorState. // Cache the `reactOutputComponentDiff` into hydrationErrorState.
......
import { import { testReactHydrationWarning } from '../is-hydration-error'
getHydrationErrorStackInfo,
testReactHydrationWarning,
} from '../is-hydration-error'
export type HydrationErrorState = { export type HydrationErrorState = {
// Hydration warning template format: <message> <serverContent> <clientContent> // Hydration warning template format: <message> <serverContent> <clientContent>
...@@ -55,14 +52,6 @@ const isHtmlTagsWarning = (message: string) => htmlTagsWarnings.has(message) ...@@ -55,14 +52,6 @@ const isHtmlTagsWarning = (message: string) => htmlTagsWarnings.has(message)
const isTextInTagsMismatchWarning = (msg: string) => const isTextInTagsMismatchWarning = (msg: string) =>
textAndTagsMismatchWarnings.has(msg) textAndTagsMismatchWarnings.has(msg)
export const getReactHydrationDiffSegments = (msg: NullableText) => {
if (msg) {
const { message, diff } = getHydrationErrorStackInfo(msg)
if (message) return [message, diff]
}
return undefined
}
/** /**
* Patch console.error to capture hydration errors. * Patch console.error to capture hydration errors.
* If any of the knownHydrationWarnings are logged, store the message and component stack. * If any of the knownHydrationWarnings are logged, store the message and component stack.
......
...@@ -52,7 +52,6 @@ export function testReactHydrationWarning(msg: string): boolean { ...@@ -52,7 +52,6 @@ export function testReactHydrationWarning(msg: string): boolean {
export function getHydrationErrorStackInfo(rawMessage: string): { export function getHydrationErrorStackInfo(rawMessage: string): {
message: string | null message: string | null
stack?: string
diff?: string diff?: string
} { } {
rawMessage = rawMessage.replace(/^Error: /, '') rawMessage = rawMessage.replace(/^Error: /, '')
...@@ -62,7 +61,6 @@ export function getHydrationErrorStackInfo(rawMessage: string): { ...@@ -62,7 +61,6 @@ export function getHydrationErrorStackInfo(rawMessage: string): {
if (!isReactHydrationErrorMessage(rawMessage) && !isReactHydrationWarning) { if (!isReactHydrationErrorMessage(rawMessage) && !isReactHydrationWarning) {
return { return {
message: null, message: null,
stack: rawMessage,
diff: '', diff: '',
} }
} }
...@@ -71,7 +69,6 @@ export function getHydrationErrorStackInfo(rawMessage: string): { ...@@ -71,7 +69,6 @@ export function getHydrationErrorStackInfo(rawMessage: string): {
const [message, diffLog] = rawMessage.split('\n\n') const [message, diffLog] = rawMessage.split('\n\n')
return { return {
message: message.trim(), message: message.trim(),
stack: '',
diff: (diffLog || '').trim(), diff: (diffLog || '').trim(),
} }
} }
...@@ -83,13 +80,10 @@ export function getHydrationErrorStackInfo(rawMessage: string): { ...@@ -83,13 +80,10 @@ export function getHydrationErrorStackInfo(rawMessage: string): {
const trimmedMessage = message.trim() const trimmedMessage = message.trim()
// React built-in hydration diff starts with a newline, checking if length is > 1 // React built-in hydration diff starts with a newline, checking if length is > 1
if (trailing && trailing.length > 1) { if (trailing && trailing.length > 1) {
const stacks: string[] = []
const diffs: string[] = [] const diffs: string[] = []
trailing.split('\n').forEach((line) => { trailing.split('\n').forEach((line) => {
if (line.trim() === '') return if (line.trim() === '') return
if (line.trim().startsWith('at ')) { if (!line.trim().startsWith('at ')) {
stacks.push(line)
} else {
diffs.push(line) diffs.push(line)
} }
}) })
...@@ -97,12 +91,10 @@ export function getHydrationErrorStackInfo(rawMessage: string): { ...@@ -97,12 +91,10 @@ export function getHydrationErrorStackInfo(rawMessage: string): {
return { return {
message: trimmedMessage, message: trimmedMessage,
diff: diffs.join('\n'), diff: diffs.join('\n'),
stack: stacks.join('\n'),
} }
} else { } else {
return { return {
message: trimmedMessage, message: trimmedMessage,
stack: trailing, // without hydration diff
} }
} }
} }
...@@ -4,7 +4,6 @@ import { useMeasureHeight } from '../../hooks/use-measure-height' ...@@ -4,7 +4,6 @@ import { useMeasureHeight } from '../../hooks/use-measure-height'
export type DialogProps = { export type DialogProps = {
children?: React.ReactNode children?: React.ReactNode
type: 'error' | 'warning'
'aria-labelledby': string 'aria-labelledby': string
'aria-describedby': string 'aria-describedby': string
className?: string className?: string
...@@ -22,7 +21,6 @@ const CSS_SELECTORS_TO_EXCLUDE_ON_CLICK_OUTSIDE = [ ...@@ -22,7 +21,6 @@ const CSS_SELECTORS_TO_EXCLUDE_ON_CLICK_OUTSIDE = [
const Dialog: React.FC<DialogProps> = function Dialog({ const Dialog: React.FC<DialogProps> = function Dialog({
children, children,
type,
className, className,
onClose, onClose,
'aria-labelledby': ariaLabelledBy, 'aria-labelledby': ariaLabelledBy,
......
...@@ -16,7 +16,6 @@ export function ErrorOverlayDialog({ ...@@ -16,7 +16,6 @@ export function ErrorOverlayDialog({
return ( return (
<div className="error-overlay-dialog-container"> <div className="error-overlay-dialog-container">
<Dialog <Dialog
type="error"
aria-labelledby="nextjs__container_errors_label" aria-labelledby="nextjs__container_errors_label"
aria-describedby="nextjs__container_errors_desc" aria-describedby="nextjs__container_errors_desc"
className="error-overlay-dialog-scroll" className="error-overlay-dialog-scroll"
......
...@@ -49,16 +49,10 @@ import { CollapseIcon } from '../../icons/collapse-icon' ...@@ -49,16 +49,10 @@ import { CollapseIcon } from '../../icons/collapse-icon'
* *
*/ */
export function PseudoHtmlDiff({ export function PseudoHtmlDiff({
firstContent,
secondContent,
hydrationMismatchType,
reactOutputComponentDiff, reactOutputComponentDiff,
...props ...props
}: { }: {
firstContent: string
secondContent: string
reactOutputComponentDiff: string reactOutputComponentDiff: string
hydrationMismatchType: 'tag' | 'text' | 'text-in-tag'
} & React.HTMLAttributes<HTMLPreElement>) { } & React.HTMLAttributes<HTMLPreElement>) {
const [isDiffCollapsed, toggleCollapseHtml] = useState(true) const [isDiffCollapsed, toggleCollapseHtml] = useState(true)
......
...@@ -5,10 +5,7 @@ import { RuntimeError } from './runtime-error' ...@@ -5,10 +5,7 @@ import { RuntimeError } from './runtime-error'
import { getErrorSource } from '../../../../../shared/lib/error-source' import { getErrorSource } from '../../../../../shared/lib/error-source'
import { HotlinkedText } from '../components/hot-linked-text' import { HotlinkedText } from '../components/hot-linked-text'
import { PseudoHtmlDiff } from './runtime-error/component-stack-pseudo-html' import { PseudoHtmlDiff } from './runtime-error/component-stack-pseudo-html'
import { import type { HydrationErrorState } from '../../../errors/hydration-error-info'
type HydrationErrorState,
getHydrationWarningType,
} from '../../../errors/hydration-error-info'
import { import {
isConsoleError, isConsoleError,
getConsoleErrorType, getConsoleErrorType,
...@@ -131,7 +128,6 @@ export function Errors({ ...@@ -131,7 +128,6 @@ export function Errors({
const [warningTemplate, serverContent, clientContent] = const [warningTemplate, serverContent, clientContent] =
errorDetails.warning || [null, '', ''] errorDetails.warning || [null, '', '']
const hydrationErrorType = getHydrationWarningType(warningTemplate)
const hydrationWarning = warningTemplate const hydrationWarning = warningTemplate
? warningTemplate ? warningTemplate
.replace('%s', serverContent) .replace('%s', serverContent)
...@@ -193,9 +189,6 @@ export function Errors({ ...@@ -193,9 +189,6 @@ export function Errors({
!!errorDetails.reactOutputComponentDiff) ? ( !!errorDetails.reactOutputComponentDiff) ? (
<PseudoHtmlDiff <PseudoHtmlDiff
className="nextjs__container_errors__component-stack" className="nextjs__container_errors__component-stack"
hydrationMismatchType={hydrationErrorType}
firstContent={serverContent}
secondContent={clientContent}
reactOutputComponentDiff={errorDetails.reactOutputComponentDiff || ''} reactOutputComponentDiff={errorDetails.reactOutputComponentDiff || ''}
/> />
) : null} ) : null}
......
...@@ -15,25 +15,18 @@ type Story = StoryObj<typeof PseudoHtmlDiff> ...@@ -15,25 +15,18 @@ type Story = StoryObj<typeof PseudoHtmlDiff>
export const TextMismatch: Story = { export const TextMismatch: Story = {
args: { args: {
firstContent: 'Server rendered content',
secondContent: 'Client rendered content',
hydrationMismatchType: 'text',
reactOutputComponentDiff: undefined, reactOutputComponentDiff: undefined,
}, },
} }
export const TextInTagMismatch: Story = { export const TextInTagMismatch: Story = {
args: { args: {
firstContent: 'Mismatched content',
secondContent: 'p',
hydrationMismatchType: 'text-in-tag',
reactOutputComponentDiff: undefined, reactOutputComponentDiff: undefined,
}, },
} }
export const ReactUnifiedMismatch: Story = { export const ReactUnifiedMismatch: Story = {
args: { args: {
hydrationMismatchType: 'tag',
reactOutputComponentDiff: `<Page> reactOutputComponentDiff: `<Page>
<Layout> <Layout>
<div> <div>
......
import { parse } from 'next/dist/compiled/stacktrace-parser' import { parse } from 'next/dist/compiled/stacktrace-parser'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import {
getHydrationErrorStackInfo,
isReactHydrationErrorMessage,
} from '../../is-hydration-error'
const regexNextStatic = /\/_next(\/static\/.+)/ const regexNextStatic = /\/_next(\/static\/.+)/
export function parseStack(stack: string | undefined): StackFrame[] { export function parseStack(stack: string | undefined): StackFrame[] {
if (!stack) return [] if (!stack) return []
const messageAndStack = stack.replace(/^Error: /, '')
if (isReactHydrationErrorMessage(messageAndStack)) {
const { stack: parsedStack } = getHydrationErrorStackInfo(messageAndStack)
if (parsedStack) {
stack = parsedStack
}
}
// throw away eval information that stacktrace-parser doesn't support // throw away eval information that stacktrace-parser doesn't support
// adapted from https://github.com/stacktracejs/error-stack-parser/blob/9f33c224b5d7b607755eb277f9d51fcdb7287e24/error-stack-parser.js#L59C33-L59C62 // adapted from https://github.com/stacktracejs/error-stack-parser/blob/9f33c224b5d7b607755eb277f9d51fcdb7287e24/error-stack-parser.js#L59C33-L59C62
......
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