-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reorganize patch console (#70374)
- Loading branch information
Showing
3 changed files
with
34 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/next/src/client/components/globals/intercept-console-error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { patchConsoleError } from './intercept-console-error' | ||
|
||
patchConsoleError() |