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
d59aa965
Unverified
Commit
d59aa965
authored
Mar 07, 2023
by
Hannes Bornö
Committed by
GitHub
Mar 07, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Port error overlay hydration error to pages directory (#46677)
Co-authored-by:
JJ Kasper
<
jj@jjsweb.site
>
parent
70d64382
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
174 additions
and
5 deletions
+174
-5
parse-component-stack.ts
...act-dev-overlay/internal/helpers/parse-component-stack.ts
+1
-1
client.ts
packages/react-dev-overlay/src/client.ts
+19
-0
bus.ts
packages/react-dev-overlay/src/internal/bus.ts
+1
-0
RuntimeError.tsx
...react-dev-overlay/src/internal/container/RuntimeError.tsx
+17
-2
getErrorByType.ts
.../react-dev-overlay/src/internal/helpers/getErrorByType.ts
+6
-1
tsconfig.json
packages/react-dev-overlay/tsconfig.json
+1
-1
local.d.ts
packages/react-dev-overlay/types/local.d.ts
+7
-0
component-stack.test.ts
test/development/acceptance/component-stack.test.ts
+62
-0
helpers.ts
test/development/acceptance/helpers.ts
+11
-0
hydration-error.test.ts
test/development/acceptance/hydration-error.test.ts
+49
-0
No files found.
packages/next/src/client/components/react-dev-overlay/internal/helpers/parse-component-stack.ts
View file @
d59aa965
...
@@ -18,7 +18,7 @@ export function parseComponentStack(
...
@@ -18,7 +18,7 @@ export function parseComponentStack(
const
webpackFile
=
match
[
3
]
const
webpackFile
=
match
[
3
]
// Stop parsing the component stack if we reach a Next.js component
// Stop parsing the component stack if we reach a Next.js component
if
(
webpackFile
?.
includes
(
'next/dist
/client/components/
'
))
{
if
(
webpackFile
?.
includes
(
'next/dist'
))
{
break
break
}
}
...
...
packages/react-dev-overlay/src/client.ts
View file @
d59aa965
import
*
as
Bus
from
'./internal/bus'
import
*
as
Bus
from
'./internal/bus'
import
{
parseStack
}
from
'./internal/helpers/parseStack'
import
{
parseStack
}
from
'./internal/helpers/parseStack'
import
{
parseComponentStack
}
from
'next/dist/client/components/react-dev-overlay/internal/helpers/parse-component-stack'
import
{
hydrationErrorComponentStack
,
hydrationErrorWarning
,
patchConsoleError
,
}
from
'next/dist/client/components/react-dev-overlay/internal/helpers/hydration-error-info'
// Patch console.error to collect information about hydration errors
patchConsoleError
()
let
isRegistered
=
false
let
isRegistered
=
false
let
stackTraceLimit
:
number
|
undefined
=
undefined
let
stackTraceLimit
:
number
|
undefined
=
undefined
...
@@ -14,14 +23,24 @@ function onUnhandledError(ev: ErrorEvent) {
...
@@ -14,14 +23,24 @@ function onUnhandledError(ev: ErrorEvent) {
if
(
if
(
error
.
message
.
match
(
/
(
hydration|content does not match|did not match
)
/i
)
error
.
message
.
match
(
/
(
hydration|content does not match|did not match
)
/i
)
)
{
)
{
if
(
hydrationErrorWarning
)
{
error
.
message
+=
'
\
n
\
n'
+
hydrationErrorWarning
}
error
.
message
+=
`\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error`
error
.
message
+=
`\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error`
}
}
const
e
=
error
const
e
=
error
const
componentStack
=
typeof
hydrationErrorComponentStack
===
'string'
?
parseComponentStack
(
hydrationErrorComponentStack
).
map
(
(
frame
:
any
)
=>
frame
.
component
)
:
undefined
Bus
.
emit
({
Bus
.
emit
({
type
:
Bus
.
TYPE_UNHANDLED_ERROR
,
type
:
Bus
.
TYPE_UNHANDLED_ERROR
,
reason
:
error
,
reason
:
error
,
frames
:
parseStack
(
e
.
stack
!
),
frames
:
parseStack
(
e
.
stack
!
),
componentStack
,
})
})
}
}
...
...
packages/react-dev-overlay/src/internal/bus.ts
View file @
d59aa965
...
@@ -18,6 +18,7 @@ export type UnhandledError = {
...
@@ -18,6 +18,7 @@ export type UnhandledError = {
type
:
typeof
TYPE_UNHANDLED_ERROR
type
:
typeof
TYPE_UNHANDLED_ERROR
reason
:
Error
reason
:
Error
frames
:
StackFrame
[]
frames
:
StackFrame
[]
componentStack
?:
string
[]
}
}
export
type
UnhandledRejection
=
{
export
type
UnhandledRejection
=
{
type
:
typeof
TYPE_UNHANDLED_REJECTION
type
:
typeof
TYPE_UNHANDLED_REJECTION
...
...
packages/react-dev-overlay/src/internal/container/RuntimeError.tsx
View file @
d59aa965
...
@@ -139,6 +139,18 @@ const RuntimeError: React.FC<RuntimeErrorProps> = function RuntimeError({
...
@@ -139,6 +139,18 @@ const RuntimeError: React.FC<RuntimeErrorProps> = function RuntimeError({
/>
/>
</
React
.
Fragment
>
</
React
.
Fragment
>
)
:
undefined
}
)
:
undefined
}
{
error
.
componentStack
?
(
<>
<
h5
>
Component Stack
</
h5
>
{
error
.
componentStack
.
map
((
component
,
index
)
=>
(
<
div
key=
{
index
}
data
-
nextjs
-
component
-
stack
-
frame
>
<
h6
>
{
component
}
</
h6
>
</
div
>
))
}
</>
)
:
null
}
{
visibleCallStackFrames
.
length
?
(
{
visibleCallStackFrames
.
length
?
(
<
React
.
Fragment
>
<
React
.
Fragment
>
<
h5
>
Call Stack
</
h5
>
<
h5
>
Call Stack
</
h5
>
...
@@ -147,6 +159,7 @@ const RuntimeError: React.FC<RuntimeErrorProps> = function RuntimeError({
...
@@ -147,6 +159,7 @@ const RuntimeError: React.FC<RuntimeErrorProps> = function RuntimeError({
))
}
))
}
</
React
.
Fragment
>
</
React
.
Fragment
>
)
:
undefined
}
)
:
undefined
}
{
canShowMore
?
(
{
canShowMore
?
(
<
React
.
Fragment
>
<
React
.
Fragment
>
<
button
<
button
...
@@ -173,11 +186,13 @@ export const styles = css`
...
@@ -173,11 +186,13 @@ export const styles = css`
color: var(--color-accents-3);
color: var(--color-accents-3);
}
}
[data-nextjs-call-stack-frame]:not(:last-child) {
[data-nextjs-call-stack-frame]:not(:last-child),
[data-nextjs-component-stack-frame]:not(:last-child) {
margin-bottom: var(--size-gap-double);
margin-bottom: var(--size-gap-double);
}
}
[data-nextjs-call-stack-frame] > h6 {
[data-nextjs-call-stack-frame] > h6,
[data-nextjs-component-stack-frame] > h6 {
margin-top: 0;
margin-top: 0;
margin-bottom: var(--size-gap);
margin-bottom: var(--size-gap);
font-family: var(--font-stack-monospace);
font-family: var(--font-stack-monospace);
...
...
packages/react-dev-overlay/src/internal/helpers/getErrorByType.ts
View file @
d59aa965
...
@@ -8,6 +8,7 @@ export type ReadyRuntimeError = {
...
@@ -8,6 +8,7 @@ export type ReadyRuntimeError = {
runtime
:
true
runtime
:
true
error
:
Error
error
:
Error
frames
:
OriginalStackFrame
[]
frames
:
OriginalStackFrame
[]
componentStack
?:
string
[]
}
}
export
async
function
getErrorByType
(
export
async
function
getErrorByType
(
...
@@ -17,7 +18,7 @@ export async function getErrorByType(
...
@@ -17,7 +18,7 @@ export async function getErrorByType(
switch
(
event
.
type
)
{
switch
(
event
.
type
)
{
case
TYPE_UNHANDLED_ERROR
:
case
TYPE_UNHANDLED_ERROR
:
case
TYPE_UNHANDLED_REJECTION
:
{
case
TYPE_UNHANDLED_REJECTION
:
{
return
{
const
readyRuntimeError
:
ReadyRuntimeError
=
{
id
,
id
,
runtime
:
true
,
runtime
:
true
,
error
:
event
.
reason
,
error
:
event
.
reason
,
...
@@ -27,6 +28,10 @@ export async function getErrorByType(
...
@@ -27,6 +28,10 @@ export async function getErrorByType(
event
.
reason
.
toString
()
event
.
reason
.
toString
()
),
),
}
}
if
(
event
.
type
===
TYPE_UNHANDLED_ERROR
)
{
readyRuntimeError
.
componentStack
=
event
.
componentStack
}
return
readyRuntimeError
}
}
default
:
{
default
:
{
break
break
...
...
packages/react-dev-overlay/tsconfig.json
View file @
d59aa965
...
@@ -14,6 +14,6 @@
...
@@ -14,6 +14,6 @@
"skipLibCheck"
:
true
,
"skipLibCheck"
:
true
,
"moduleResolution"
:
"Node16"
"moduleResolution"
:
"Node16"
},
},
"include"
:
[
"src/**/*.ts"
,
"src/**/*.tsx"
],
"include"
:
[
"src/**/*.ts"
,
"src/**/*.tsx"
,
"types/local.d.ts"
],
"exclude"
:
[
"node_modules"
]
"exclude"
:
[
"node_modules"
]
}
}
packages/react-dev-overlay/types/local.d.ts
0 → 100644
View file @
d59aa965
declare
module
'next/dist/client/components/react-dev-overlay/internal/helpers/parse-component-stack'
{
export
*
from
'next/src/client/components/react-dev-overlay/internal/helpers/parse-component-stack'
}
declare
module
'next/dist/client/components/react-dev-overlay/internal/helpers/hydration-error-info'
{
export
*
from
'next/src/client/components/react-dev-overlay/internal/helpers/hydration-error-info'
}
test/development/acceptance/component-stack.test.ts
0 → 100644
View file @
d59aa965
/* eslint-env jest */
import
{
sandbox
}
from
'./helpers'
import
{
createNextDescribe
}
from
'e2e-utils'
createNextDescribe
(
'Component Stack in error overlay'
,
{
files
:
{},
dependencies
:
{
react
:
'latest'
,
'react-dom'
:
'latest'
,
},
skipStart
:
true
,
},
({
next
})
=>
{
it
(
'should show a component stack on hydration error'
,
async
()
=>
{
const
{
cleanup
,
session
}
=
await
sandbox
(
next
,
new
Map
([
[
'component.js'
,
`
const isClient = typeof window !== 'undefined'
export default function Component() {
return (
<div>
<p>{isClient ? "client" : "server"}</p>
</div>
);
}
`
,
],
[
'index.js'
,
`
import Component from './component'
export default function Mismatch() {
return (
<main>
<Component />
</main>
);
}
`
,
],
])
)
expect
(
await
session
.
hasRedbox
(
true
)).
toBe
(
true
)
expect
(
await
session
.
getRedboxComponentStack
()).
toMatchInlineSnapshot
(
`
"p
div
Component
main
Mismatch"
`
)
await
cleanup
()
})
}
)
test/development/acceptance/helpers.ts
View file @
d59aa965
...
@@ -120,6 +120,17 @@ export async function sandbox(
...
@@ -120,6 +120,17 @@ export async function sandbox(
}
}
return
source
return
source
},
},
async
getRedboxComponentStack
()
{
await
browser
.
waitForElementByCss
(
'[data-nextjs-component-stack-frame]'
)
const
componentStackFrameElements
=
await
browser
.
elementsByCss
(
'[data-nextjs-component-stack-frame]'
)
const
componentStackFrameTexts
=
await
Promise
.
all
(
componentStackFrameElements
.
map
((
f
)
=>
f
.
innerText
())
)
return
componentStackFrameTexts
.
join
(
'
\
n'
)
},
},
},
async
cleanup
()
{
async
cleanup
()
{
await
browser
.
close
()
await
browser
.
close
()
...
...
test/development/acceptance/hydration-error.test.ts
0 → 100644
View file @
d59aa965
/* eslint-env jest */
import
{
sandbox
}
from
'./helpers'
import
{
createNextDescribe
}
from
'e2e-utils'
createNextDescribe
(
'Error overlay for hydration errors'
,
{
files
:
{},
dependencies
:
{
react
:
'latest'
,
'react-dom'
:
'latest'
,
},
skipStart
:
true
,
},
({
next
})
=>
{
it
(
'should show correct hydration error when client and server render different text'
,
async
()
=>
{
const
{
cleanup
,
session
}
=
await
sandbox
(
next
,
new
Map
([
[
'index.js'
,
`
const isClient = typeof window !== 'undefined'
export default function Mismatch() {
return (
<div className="parent">
<main className="child">{isClient ? "client" : "server"}</main>
</div>
);
}
`
,
],
])
)
expect
(
await
session
.
hasRedbox
(
true
)).
toBe
(
true
)
expect
(
await
session
.
getRedboxDescription
()).
toMatchInlineSnapshot
(
`
"Error: Text content does not match server-rendered HTML.
Warning: Text content did not match. Server: \\"server\\" Client: \\"client\\"
See more info here: https://nextjs.org/docs/messages/react-hydration-error"
`
)
await
cleanup
()
})
}
)
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