Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
9b39c79d
Unverified
Commit
9b39c79d
authored
Jan 19, 2023
by
Hannes Bornö
Committed by
GitHub
Jan 19, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Display the reason of hydration error in error overlay (#44857)
parent
d363e6f2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
1316 additions
and
27 deletions
+1316
-27
app-index.tsx
packages/next/src/client/app-index.tsx
+11
-0
hydration-error-info.ts
...eact-dev-overlay/internal/helpers/hydration-error-info.ts
+30
-0
use-error-handler.ts
...s/react-dev-overlay/internal/helpers/use-error-handler.ts
+6
-1
ReactRefreshLogBox.test.ts
test/development/acceptance-app/ReactRefreshLogBox.test.ts
+0
-20
ReactRefreshLogBox.test.ts.snap
...eptance-app/__snapshots__/ReactRefreshLogBox.test.ts.snap
+0
-6
hydration-error.test.ts
test/development/acceptance-app/hydration-error.test.ts
+1269
-0
No files found.
packages/next/src/client/app-index.tsx
View file @
9b39c79d
...
@@ -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
(()
=>
...
...
packages/next/src/client/components/react-dev-overlay/internal/helpers/hydration-error-info.ts
0 → 100644
View file @
9b39c79d
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
)
}
}
packages/next/src/client/components/react-dev-overlay/internal/helpers/use-error-handler.ts
View file @
9b39c79d
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
...
...
test/development/acceptance-app/ReactRefreshLogBox.test.ts
View file @
9b39c79d
...
@@ -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
,
...
...
test/development/acceptance-app/__snapshots__/ReactRefreshLogBox.test.ts.snap
View file @
9b39c79d
// 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'
...
...
test/development/acceptance-app/hydration-error.test.ts
0 → 100644
View file @
9b39c79d
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment