From d1689519c309b342531edf29c1dcb99d492571a0 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 10 Jul 2024 21:35:21 +0200 Subject: [PATCH] separate util into one module --- .../helpers/attach-hydration-error-state.ts | 48 +++++++++++++++++ .../internal/helpers/use-error-handler.ts | 53 ++----------------- .../react-dev-overlay/pages/client.ts | 2 +- 3 files changed, 52 insertions(+), 51 deletions(-) create mode 100644 packages/next/src/client/components/react-dev-overlay/internal/helpers/attach-hydration-error-state.ts diff --git a/packages/next/src/client/components/react-dev-overlay/internal/helpers/attach-hydration-error-state.ts b/packages/next/src/client/components/react-dev-overlay/internal/helpers/attach-hydration-error-state.ts new file mode 100644 index 00000000000000..e9e8b206550a15 --- /dev/null +++ b/packages/next/src/client/components/react-dev-overlay/internal/helpers/attach-hydration-error-state.ts @@ -0,0 +1,48 @@ +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 + } +} diff --git a/packages/next/src/client/components/react-dev-overlay/internal/helpers/use-error-handler.ts b/packages/next/src/client/components/react-dev-overlay/internal/helpers/use-error-handler.ts index a2d9fb81d82658..d8006f6cf00628 100644 --- a/packages/next/src/client/components/react-dev-overlay/internal/helpers/use-error-handler.ts +++ b/packages/next/src/client/components/react-dev-overlay/internal/helpers/use-error-handler.ts @@ -1,13 +1,8 @@ import { useEffect } from 'react' -import { - hydrationErrorState, - getReactHydrationDiffSegments, -} from './hydration-error-info' + import { isNextRouterError } from '../../../is-next-router-error' -import { - isHydrationError, - getDefaultHydrationErrorMessage, -} from '../../../is-hydration-error' +import { isHydrationError } from '../../../is-hydration-error' +import { attachHydrationErrorState } from './attach-hydration-error-state' export type ErrorHandler = (error: Error) => void @@ -24,48 +19,6 @@ const rejectionQueue: Array = [] const errorHandlers: Array = [] const rejectionHandlers: Array = [] -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 - } - - return error -} - export function handleClientError(error: unknown) { if (!error || !(error instanceof Error) || typeof error.stack !== 'string') { // A non-error was thrown, we don't have anything to show. :-( diff --git a/packages/next/src/client/components/react-dev-overlay/pages/client.ts b/packages/next/src/client/components/react-dev-overlay/pages/client.ts index eb32eef108b9ec..e10ddbd1b15940 100644 --- a/packages/next/src/client/components/react-dev-overlay/pages/client.ts +++ b/packages/next/src/client/components/react-dev-overlay/pages/client.ts @@ -15,7 +15,7 @@ import { ACTION_VERSION_INFO, } from '../shared' import type { VersionInfo } from '../../../../server/dev/parse-version-info' -import { attachHydrationErrorState } from '../internal/helpers/use-error-handler' +import { attachHydrationErrorState } from '../internal/helpers/attach-hydration-error-state' let isRegistered = false let stackTraceLimit: number | undefined = undefined