Unverified Commit 9b39c79d authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Display the reason of hydration error in error overlay (#44857)

parent d363e6f2
...@@ -254,6 +254,17 @@ export function hydrate() { ...@@ -254,6 +254,17 @@ export function hydrate() {
onRecoverableError, onRecoverableError,
} }
const isError = document.documentElement.id === '__next_error__' const isError = document.documentElement.id === '__next_error__'
if (process.env.NODE_ENV !== 'production') {
// Patch console.error to collect information about hydration errors
const patchConsoleError =
require('./components/react-dev-overlay/internal/helpers/hydration-error-info')
.patchConsoleError as typeof import('./components/react-dev-overlay/internal/helpers/hydration-error-info').patchConsoleError
if (!isError) {
patchConsoleError()
}
}
const reactRoot = isError const reactRoot = isError
? (ReactDOMClient as any).createRoot(appElement, options) ? (ReactDOMClient as any).createRoot(appElement, options)
: (React as any).startTransition(() => : (React as any).startTransition(() =>
......
export let hydrationErrorInfo: string | undefined
const knownHydrationWarnings = new Set([
'Warning: Text content did not match. Server: "%s" Client: "%s"%s',
'Warning: Expected server HTML to contain a matching <%s> in <%s>.%s',
'Warning: Expected server HTML to contain a matching text node for "%s" in <%s>.%s',
'Warning: Did not expect server HTML to contain a <%s> in <%s>.%s',
'Warning: Did not expect server HTML to contain the text node "%s" in <%s>.%s',
])
export function patchConsoleError() {
const prev = console.error
console.error = function (
msg,
serverContent,
clientContent,
// TODO-APP: Display the component stack in the overlay
_componentStack
) {
if (knownHydrationWarnings.has(msg)) {
hydrationErrorInfo = msg
.replace('%s', serverContent)
.replace('%s', clientContent)
.replace('%s', '')
}
// @ts-expect-error argument is defined
prev.apply(console, arguments)
}
}
import { useEffect } from 'react' import { useEffect } from 'react'
import { hydrationErrorInfo } from './hydration-error-info'
export type ErrorHandler = (error: Error) => void export type ErrorHandler = (error: Error) => void
...@@ -60,7 +61,11 @@ if (typeof window !== 'undefined') { ...@@ -60,7 +61,11 @@ if (typeof window !== 'undefined') {
'https://nextjs.org/docs/messages/react-hydration-error' 'https://nextjs.org/docs/messages/react-hydration-error'
) )
) { ) {
error.message += `\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error` if (hydrationErrorInfo) {
error.message += '\n\n' + hydrationErrorInfo
}
error.message +=
'\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error'
} }
const e = error const e = error
......
...@@ -1346,26 +1346,6 @@ for (const variant of ['default', 'turbo']) { ...@@ -1346,26 +1346,6 @@ for (const variant of ['default', 'turbo']) {
await cleanup() await cleanup()
}) })
test('Hydration errors should get error link', async () => {
const { session, browser, cleanup } = await sandbox(next)
await session.patch(
'app/page.js',
`
"use client"
export default function Page() {
return <p>{typeof window === 'undefined' ? "hello" : "world"}</p>
}
`
)
await browser.refresh()
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxDescription()).toMatchSnapshot()
await cleanup()
})
test('Import trace when module not found in layout', async () => { test('Import trace when module not found in layout', async () => {
const { session, cleanup } = await sandbox( const { session, cleanup } = await sandbox(
next, next,
......
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ReactRefreshLogBox app default Hydration errors should get error link 1`] = `
"Error: Text content does not match server-rendered HTML.
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`;
exports[`ReactRefreshLogBox app default Import trace when module not found in layout 1`] = ` exports[`ReactRefreshLogBox app default Import trace when module not found in layout 1`] = `
"./app/module.js:1:0 "./app/module.js:1:0
Module not found: Can't resolve 'non-existing-module' Module not found: Can't resolve 'non-existing-module'
......
This diff is collapsed.
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