Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Sep 24, 2024
1 parent 3c66cbe commit 2475da8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { isNextRouterError } from '../is-next-router-error'
import { handleClientError } from '../react-dev-overlay/internal/helpers/use-error-handler'

// Patch console.error to collect information about hydration errors
export function patchConsoleError() {
// Ensure it's only patched once
if (typeof window === 'undefined') {
return
}

const originConsoleError = window.console.error
window.console.error = (...args) => {
// See https://github.com/facebook/react/blob/d50323eb845c5fde0d720cae888bf35dedd05506/packages/react-reconciler/src/ReactFiberErrorLogger.js#L78
const error = process.env.NODE_ENV !== 'production' ? args[1] : args[0]

if (!isNextRouterError(error)) {
if (process.env.NODE_ENV !== 'production') {
const { storeHydrationErrorStateFromConsoleArgs } =
require('../react-dev-overlay/internal/helpers/hydration-error-info') as typeof import('../react-dev-overlay/internal/helpers/hydration-error-info')

storeHydrationErrorStateFromConsoleArgs(...args)
handleClientError(error)
}

originConsoleError.apply(window.console, args)
}
}
}
36 changes: 1 addition & 35 deletions packages/next/src/client/components/globals/patch-console.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
import { isNextRouterError } from '../is-next-router-error'
import { handleClientError } from '../react-dev-overlay/internal/helpers/use-error-handler'

const isReactOwnerStackEnabled = !!process.env.__NEXT_REACT_OWNER_STACK

// Patch console.error to collect information about hydration errors
function patchConsoleError() {
// Ensure it's only patched once
if (typeof window === 'undefined') {
return
}

const originConsoleError = window.console.error
window.console.error = (...args) => {
// See https://github.com/facebook/react/blob/d50323eb845c5fde0d720cae888bf35dedd05506/packages/react-reconciler/src/ReactFiberErrorLogger.js#L78
const error =
process.env.NODE_ENV !== 'production'
? isReactOwnerStackEnabled
? args[1] || args[0]
: args[1]
: args[0]

if (!isNextRouterError(error)) {
if (process.env.NODE_ENV !== 'production') {
const { storeHydrationErrorStateFromConsoleArgs } =
require('../react-dev-overlay/internal/helpers/hydration-error-info') as typeof import('../react-dev-overlay/internal/helpers/hydration-error-info')

storeHydrationErrorStateFromConsoleArgs(...args)
handleClientError(error)
}

originConsoleError.apply(window.console, args)
}
}
}
import { patchConsoleError } from './intercept-console-error'

patchConsoleError()

0 comments on commit 2475da8

Please sign in to comment.