Unverified Commit 026f8873 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Share the hydration error info extraction util (#67639)

### What

Share the util of extract hydration error info used for dev error
overlay into one shared util between pages router and app router
parent 3cce038a
import {
isHydrationError,
getDefaultHydrationErrorMessage,
} from '../../../is-hydration-error'
import {
hydrationErrorState,
getReactHydrationDiffSegments,
} from './hydration-error-info'
export function attachHydrationErrorState(error: Error) {
if (
isHydrationError(error) &&
!error.message.includes(
'https://nextjs.org/docs/messages/react-hydration-error'
)
) {
const reactHydrationDiffSegments = getReactHydrationDiffSegments(
error.message
)
let parsedHydrationErrorState: typeof hydrationErrorState = {}
if (reactHydrationDiffSegments) {
parsedHydrationErrorState = {
...(error as any).details,
...hydrationErrorState,
warning: hydrationErrorState.warning || [
getDefaultHydrationErrorMessage(),
],
notes: reactHydrationDiffSegments[0],
reactOutputComponentDiff: reactHydrationDiffSegments[1],
}
} else {
// If there's any extra information in the error message to display,
// append it to the error message details property
if (hydrationErrorState.warning) {
// The patched console.error found hydration errors logged by React
// Append the logged warning to the error message
parsedHydrationErrorState = {
...(error as any).details,
// It contains the warning, component stack, server and client tag names
...hydrationErrorState,
}
}
error.message +=
'\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error'
}
;(error as any).details = parsedHydrationErrorState
}
}
import { useEffect } from 'react' import { useEffect } from 'react'
import {
hydrationErrorState,
getReactHydrationDiffSegments,
} from './hydration-error-info'
import { isNextRouterError } from '../../../is-next-router-error' import { isNextRouterError } from '../../../is-next-router-error'
import { import { isHydrationError } from '../../../is-hydration-error'
isHydrationError, import { attachHydrationErrorState } from './attach-hydration-error-state'
getDefaultHydrationErrorMessage,
} from '../../../is-hydration-error'
export type ErrorHandler = (error: Error) => void export type ErrorHandler = (error: Error) => void
...@@ -30,47 +25,10 @@ export function handleClientError(error: unknown) { ...@@ -30,47 +25,10 @@ export function handleClientError(error: unknown) {
return return
} }
const isCausedByHydrationFailure = isHydrationError(error) attachHydrationErrorState(error)
if (
isHydrationError(error) &&
!error.message.includes(
'https://nextjs.org/docs/messages/react-hydration-error'
)
) {
const reactHydrationDiffSegments = getReactHydrationDiffSegments(
error.message
)
let parsedHydrationErrorState: typeof hydrationErrorState = {}
if (reactHydrationDiffSegments) {
parsedHydrationErrorState = {
...(error as any).details,
...hydrationErrorState,
warning: hydrationErrorState.warning || [
getDefaultHydrationErrorMessage(),
],
notes: reactHydrationDiffSegments[0],
reactOutputComponentDiff: reactHydrationDiffSegments[1],
}
} else {
// If there's any extra information in the error message to display,
// append it to the error message details property
if (hydrationErrorState.warning) {
// The patched console.error found hydration errors logged by React
// Append the logged warning to the error message
parsedHydrationErrorState = {
...(error as any).details,
// It contains the warning, component stack, server and client tag names
...hydrationErrorState,
}
}
error.message +=
'\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error'
}
;(error as any).details = parsedHydrationErrorState
}
// Only queue one hydration every time // Only queue one hydration every time
if (isCausedByHydrationFailure) { if (isHydrationError(error)) {
if (!hasHydrationError) { if (!hasHydrationError) {
errorQueue.push(error) errorQueue.push(error)
} }
......
...@@ -2,7 +2,6 @@ import * as Bus from './bus' ...@@ -2,7 +2,6 @@ import * as Bus from './bus'
import { parseStack } from '../internal/helpers/parseStack' import { parseStack } from '../internal/helpers/parseStack'
import { parseComponentStack } from '../internal/helpers/parse-component-stack' import { parseComponentStack } from '../internal/helpers/parse-component-stack'
import { import {
getReactHydrationDiffSegments,
hydrationErrorState, hydrationErrorState,
storeHydrationErrorStateFromConsoleArgs, storeHydrationErrorStateFromConsoleArgs,
} from '../internal/helpers/hydration-error-info' } from '../internal/helpers/hydration-error-info'
...@@ -16,10 +15,7 @@ import { ...@@ -16,10 +15,7 @@ import {
ACTION_VERSION_INFO, ACTION_VERSION_INFO,
} from '../shared' } from '../shared'
import type { VersionInfo } from '../../../../server/dev/parse-version-info' import type { VersionInfo } from '../../../../server/dev/parse-version-info'
import { import { attachHydrationErrorState } from '../internal/helpers/attach-hydration-error-state'
getDefaultHydrationErrorMessage,
isHydrationError,
} from '../../is-hydration-error'
let isRegistered = false let isRegistered = false
let stackTraceLimit: number | undefined = undefined let stackTraceLimit: number | undefined = undefined
...@@ -30,43 +26,8 @@ function handleError(error: unknown) { ...@@ -30,43 +26,8 @@ function handleError(error: unknown) {
return return
} }
if ( attachHydrationErrorState(error)
isHydrationError(error) &&
!error.message.includes(
'https://nextjs.org/docs/messages/react-hydration-error'
)
) {
const reactHydrationDiffSegments = getReactHydrationDiffSegments(
error.message
)
let parsedHydrationErrorState: typeof hydrationErrorState = {}
if (reactHydrationDiffSegments) {
parsedHydrationErrorState = {
...(error as any).details,
warning: hydrationErrorState.warning || [
getDefaultHydrationErrorMessage(),
],
notes: reactHydrationDiffSegments[0],
reactOutputComponentDiff: reactHydrationDiffSegments[1],
}
} else {
// If there's any extra information in the error message to display,
// append it to the error message details property
if (hydrationErrorState.warning) {
// The patched console.error found hydration errors logged by React
// Append the logged warning to the error message
parsedHydrationErrorState = {
...(error as any).details,
// It contains the warning, component stack, server and client tag names
...hydrationErrorState,
}
}
error.message += `\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error`
}
;(error as any).details = parsedHydrationErrorState
}
const e = error
const componentStackTrace = const componentStackTrace =
(error as any)._componentStack || hydrationErrorState.componentStack (error as any)._componentStack || hydrationErrorState.componentStack
const componentStackFrames = const componentStackFrames =
...@@ -76,11 +37,14 @@ function handleError(error: unknown) { ...@@ -76,11 +37,14 @@ function handleError(error: unknown) {
// Skip ModuleBuildError and ModuleNotFoundError, as it will be sent through onBuildError callback. // Skip ModuleBuildError and ModuleNotFoundError, as it will be sent through onBuildError callback.
// This is to avoid same error as different type showing up on client to cause flashing. // This is to avoid same error as different type showing up on client to cause flashing.
if (e.name !== 'ModuleBuildError' && e.name !== 'ModuleNotFoundError') { if (
error.name !== 'ModuleBuildError' &&
error.name !== 'ModuleNotFoundError'
) {
Bus.emit({ Bus.emit({
type: ACTION_UNHANDLED_ERROR, type: ACTION_UNHANDLED_ERROR,
reason: error, reason: error,
frames: parseStack(e.stack!), frames: parseStack(error.stack!),
componentStackFrames, componentStackFrames,
}) })
} }
......
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