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
a78163dc
Unverified
Commit
a78163dc
authored
Oct 11, 2022
by
Jiachi Liu
Committed by
GitHub
Oct 11, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Global layouts error boundary (#41305)
parent
3c8727dc
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
228 additions
and
108 deletions
+228
-108
app-router.client.tsx
packages/next/client/components/app-router.client.tsx
+18
-7
error-boundary.tsx
packages/next/client/components/error-boundary.tsx
+108
-0
layout-router.client.tsx
packages/next/client/components/layout-router.client.tsx
+2
-60
error.js
test/e2e/app-dir/app/app/error/client-component/error.js
+0
-0
page.js
test/e2e/app-dir/app/app/error/client-component/page.js
+0
-0
page.js
test/e2e/app-dir/app/app/error/global-error-boundary/page.js
+20
-0
index.test.ts
test/e2e/app-dir/index.test.ts
+80
-41
No files found.
packages/next/client/components/app-router.client.tsx
View file @
a78163dc
...
@@ -28,6 +28,7 @@ import {
...
@@ -28,6 +28,7 @@ import {
// LayoutSegmentsContext,
// LayoutSegmentsContext,
}
from
'./hooks-client-context'
}
from
'./hooks-client-context'
import
{
useReducerWithReduxDevtools
}
from
'./use-reducer-with-devtools'
import
{
useReducerWithReduxDevtools
}
from
'./use-reducer-with-devtools'
import
{
ErrorBoundary
,
GlobalErrorComponent
}
from
'./error-boundary'
function
urlToUrlWithoutFlightMarker
(
url
:
string
):
URL
{
function
urlToUrlWithoutFlightMarker
(
url
:
string
):
URL
{
const
urlWithoutFlightParameters
=
new
URL
(
url
,
location
.
origin
)
const
urlWithoutFlightParameters
=
new
URL
(
url
,
location
.
origin
)
...
@@ -91,20 +92,22 @@ let initialParallelRoutes: CacheNode['parallelRoutes'] =
...
@@ -91,20 +92,22 @@ let initialParallelRoutes: CacheNode['parallelRoutes'] =
const
prefetched
=
new
Set
<
string
>
()
const
prefetched
=
new
Set
<
string
>
()
type
AppRouterProps
=
{
initialTree
:
FlightRouterState
initialCanonicalUrl
:
string
children
:
ReactNode
assetPrefix
:
string
}
/**
/**
* The global router that wraps the application components.
* The global router that wraps the application components.
*/
*/
export
default
function
App
Router
({
function
Router
({
initialTree
,
initialTree
,
initialCanonicalUrl
,
initialCanonicalUrl
,
children
,
children
,
assetPrefix
,
assetPrefix
,
}:
{
}:
AppRouterProps
)
{
initialTree
:
FlightRouterState
initialCanonicalUrl
:
string
children
:
ReactNode
assetPrefix
:
string
})
{
const
initialState
=
useMemo
(()
=>
{
const
initialState
=
useMemo
(()
=>
{
return
{
return
{
tree
:
initialTree
,
tree
:
initialTree
,
...
@@ -367,3 +370,11 @@ export default function AppRouter({
...
@@ -367,3 +370,11 @@ export default function AppRouter({
</
PathnameContext
.
Provider
>
</
PathnameContext
.
Provider
>
)
)
}
}
export
default
function
AppRouter
(
props
:
AppRouterProps
)
{
return
(
<
ErrorBoundary
errorComponent=
{
GlobalErrorComponent
}
>
<
Router
{
...
props
}
/>
</
ErrorBoundary
>
)
}
packages/next/client/components/error-boundary.tsx
0 → 100644
View file @
a78163dc
import
React
from
'react'
export
type
ErrorComponent
=
React
.
ComponentType
<
{
error
:
Error
reset
:
()
=>
void
}
>
interface
ErrorBoundaryProps
{
errorComponent
:
ErrorComponent
}
/**
* Handles errors through `getDerivedStateFromError`.
* Renders the provided error component and provides a way to `reset` the error boundary state.
*/
class
ErrorBoundaryHandler
extends
React
.
Component
<
ErrorBoundaryProps
,
{
error
:
Error
|
null
}
>
{
constructor
(
props
:
ErrorBoundaryProps
)
{
super
(
props
)
this
.
state
=
{
error
:
null
}
}
static
getDerivedStateFromError
(
error
:
Error
)
{
return
{
error
}
}
reset
=
()
=>
{
this
.
setState
({
error
:
null
})
}
render
()
{
if
(
this
.
state
.
error
)
{
return
(
<
this
.
props
.
errorComponent
error=
{
this
.
state
.
error
}
reset=
{
this
.
reset
}
/>
)
}
return
this
.
props
.
children
}
}
/**
* Renders error boundary with the provided "errorComponent" property as the fallback.
* If no "errorComponent" property is provided it renders the children without an error boundary.
*/
export
function
ErrorBoundary
({
errorComponent
,
children
,
}:
ErrorBoundaryProps
&
{
children
:
React
.
ReactNode
}):
JSX
.
Element
{
if
(
errorComponent
)
{
return
(
<
ErrorBoundaryHandler
errorComponent=
{
errorComponent
}
>
{
children
}
</
ErrorBoundaryHandler
>
)
}
return
<>
{
children
}
</>
}
const
styles
:
{
[
k
:
string
]:
React
.
CSSProperties
}
=
{
error
:
{
fontFamily
:
'-apple-system, BlinkMacSystemFont, Roboto, "Segoe UI", "Fira Sans", Avenir, "Helvetica Neue", "Lucida Grande", sans-serif'
,
height
:
'100vh'
,
textAlign
:
'center'
,
display
:
'flex'
,
flexDirection
:
'column'
,
alignItems
:
'center'
,
justifyContent
:
'center'
,
},
desc
:
{
display
:
'inline-block'
,
textAlign
:
'left'
,
lineHeight
:
'49px'
,
height
:
'49px'
,
verticalAlign
:
'middle'
,
},
h2
:
{
fontSize
:
'14px'
,
fontWeight
:
'normal'
,
lineHeight
:
'49px'
,
margin
:
0
,
padding
:
0
,
},
}
export
function
GlobalErrorComponent
()
{
return
(
<
html
>
<
body
>
<
div
style=
{
styles
.
error
}
>
<
div
style=
{
styles
.
desc
}
>
<
h2
style=
{
styles
.
h2
}
>
Application error: a client-side exception has occurred (see the
browser console for more information).
</
h2
>
</
div
>
</
div
>
</
body
>
</
html
>
)
}
packages/next/client/components/layout-router.client.tsx
View file @
a78163dc
...
@@ -20,6 +20,7 @@ import type {
...
@@ -20,6 +20,7 @@ import type {
FlightSegmentPath
,
FlightSegmentPath
,
// FlightDataPath,
// FlightDataPath,
}
from
'../../server/app-render'
}
from
'../../server/app-render'
import
type
{
ErrorComponent
}
from
'./error-boundary'
import
{
import
{
LayoutRouterContext
,
LayoutRouterContext
,
GlobalLayoutRouterContext
,
GlobalLayoutRouterContext
,
...
@@ -28,7 +29,7 @@ import {
...
@@ -28,7 +29,7 @@ import {
}
from
'../../shared/lib/app-router-context'
}
from
'../../shared/lib/app-router-context'
import
{
fetchServerResponse
}
from
'./app-router.client'
import
{
fetchServerResponse
}
from
'./app-router.client'
import
{
createInfinitePromise
}
from
'./infinite-promise'
import
{
createInfinitePromise
}
from
'./infinite-promise'
import
{
ErrorBoundary
}
from
'./error-boundary'
import
{
matchSegment
}
from
'./match-segments'
import
{
matchSegment
}
from
'./match-segments'
/**
/**
...
@@ -364,65 +365,6 @@ function NotFoundBoundary({ notFound, children }: NotFoundBoundaryProps) {
...
@@ -364,65 +365,6 @@ function NotFoundBoundary({ notFound, children }: NotFoundBoundaryProps) {
)
)
}
}
type
ErrorComponent
=
React
.
ComponentType
<
{
error
:
Error
;
reset
:
()
=>
void
}
>
interface
ErrorBoundaryProps
{
errorComponent
:
ErrorComponent
}
/**
* Handles errors through `getDerivedStateFromError`.
* Renders the provided error component and provides a way to `reset` the error boundary state.
*/
class
ErrorBoundaryHandler
extends
React
.
Component
<
ErrorBoundaryProps
,
{
error
:
Error
|
null
}
>
{
constructor
(
props
:
ErrorBoundaryProps
)
{
super
(
props
)
this
.
state
=
{
error
:
null
}
}
static
getDerivedStateFromError
(
error
:
Error
)
{
return
{
error
}
}
reset
=
()
=>
{
this
.
setState
({
error
:
null
})
}
render
()
{
if
(
this
.
state
.
error
)
{
return
(
<
this
.
props
.
errorComponent
error=
{
this
.
state
.
error
}
reset=
{
this
.
reset
}
/>
)
}
return
this
.
props
.
children
}
}
/**
* Renders error boundary with the provided "errorComponent" property as the fallback.
* If no "errorComponent" property is provided it renders the children without an error boundary.
*/
function
ErrorBoundary
({
errorComponent
,
children
,
}:
ErrorBoundaryProps
&
{
children
:
React
.
ReactNode
}):
JSX
.
Element
{
if
(
errorComponent
)
{
return
(
<
ErrorBoundaryHandler
errorComponent=
{
errorComponent
}
>
{
children
}
</
ErrorBoundaryHandler
>
)
}
return
<>
{
children
}
</>
}
/**
/**
* OuterLayoutRouter handles the current segment as well as <Offscreen> rendering of other segments.
* OuterLayoutRouter handles the current segment as well as <Offscreen> rendering of other segments.
* It can be rendered next to each other with a different `parallelRouterKey`, allowing for Parallel routes.
* It can be rendered next to each other with a different `parallelRouterKey`, allowing for Parallel routes.
...
...
test/e2e/app-dir/app/app/error/clientcomponent/error.js
→
test/e2e/app-dir/app/app/error/client
-
component/error.js
View file @
a78163dc
File moved
test/e2e/app-dir/app/app/error/clientcomponent/page.js
→
test/e2e/app-dir/app/app/error/client
-
component/page.js
View file @
a78163dc
File moved
test/e2e/app-dir/app/app/error/global-error-boundary/page.js
0 → 100644
View file @
a78163dc
'client'
import
{
useState
}
from
'react'
export
default
function
Page
()
{
const
[
clicked
,
setClicked
]
=
useState
(
false
)
if
(
clicked
)
{
throw
new
Error
(
'this is a test'
)
}
return
(
<
button
id
=
"error-trigger-button"
onClick
=
{()
=>
{
setClicked
(
true
)
}}
>
Trigger
Error
!
<
/button
>
)
}
test/e2e/app-dir/index.test.ts
View file @
a78163dc
import
{
createNext
,
FileRef
}
from
'e2e-utils'
import
{
createNext
,
FileRef
}
from
'e2e-utils'
import
crypto
from
'crypto'
import
crypto
from
'crypto'
import
{
NextInstance
}
from
'test/lib/next-modes/base'
import
{
NextInstance
}
from
'test/lib/next-modes/base'
import
{
check
,
fetchViaHTTP
,
renderViaHTTP
,
waitFor
}
from
'next-test-utils'
import
{
check
,
fetchViaHTTP
,
getRedboxHeader
,
hasRedbox
,
renderViaHTTP
,
waitFor
,
}
from
'next-test-utils'
import
path
from
'path'
import
path
from
'path'
import
cheerio
from
'cheerio'
import
cheerio
from
'cheerio'
import
webdriver
from
'next-webdriver'
import
webdriver
from
'next-webdriver'
...
@@ -1494,57 +1501,89 @@ describe('app dir', () => {
...
@@ -1494,57 +1501,89 @@ describe('app dir', () => {
)
)
})
})
// TODO-APP: This is disabled for development as the error overlay needs to be reworked.
describe
(
'error component'
,
()
=>
{
;(
isDev
?
describe
.
skip
:
describe
)(
'error component'
,
()
=>
{
it
(
'should trigger error component when an error happens during rendering'
,
async
()
=>
{
it
(
'should trigger error component when an error happens during rendering'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/error/clientcomponent'
)
const
browser
=
await
webdriver
(
next
.
url
,
'/error/client-component'
)
await
browser
await
browser
.
elementByCss
(
'#error-trigger-button'
).
click
()
.
elementByCss
(
'#error-trigger-button'
)
.
click
()
if
(
isDev
)
{
.
waitForElementByCss
(
'#error-boundary-message'
)
expect
(
await
hasRedbox
(
browser
)).
toBe
(
true
)
console
.
log
(
'getRedboxHeader'
,
await
getRedboxHeader
(
browser
))
expect
(
// expect(await getRedboxHeader(browser)).toMatch(/An error occurred: this is a test/)
await
browser
.
elementByCss
(
'#error-boundary-message'
).
text
()
}
else
{
).
toBe
(
'An error occurred: this is a test'
)
})
it
(
'should allow resetting error boundary'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/error/clientcomponent'
)
// Try triggering and resetting a few times in a row
for
(
let
i
=
0
;
i
<
5
;
i
++
)
{
await
browser
await
browser
.
elementByCss
(
'#error-trigger-button'
)
.
click
()
.
waitForElementByCss
(
'#error-boundary-message'
)
expect
(
expect
(
await
browser
.
elementByCss
(
'#error-boundary-message'
).
text
()
await
browser
.
waitForElementByCss
(
'#error-boundary-message'
)
.
elementByCss
(
'#error-boundary-message'
)
.
text
()
).
toBe
(
'An error occurred: this is a test'
)
).
toBe
(
'An error occurred: this is a test'
)
await
browser
.
elementByCss
(
'#reset'
)
.
click
()
.
waitForElementByCss
(
'#error-trigger-button'
)
expect
(
await
browser
.
elementByCss
(
'#error-trigger-button'
).
text
()
).
toBe
(
'Trigger Error!'
)
}
}
})
})
it
(
'should
hydrate empty shell to handle server-side rendering errors
'
,
async
()
=>
{
it
(
'should
use default error boundary for prod and overlay for dev when no error component specified
'
,
async
()
=>
{
const
browser
=
await
webdriver
(
const
browser
=
await
webdriver
(
next
.
url
,
next
.
url
,
'/error/
ssr-error-client-component
'
'/error/
global-error-boundary
'
)
)
const
logs
=
await
browser
.
log
()
await
browser
.
elementByCss
(
'#error-trigger-button'
).
click
()
const
errors
=
logs
// .waitForElementByCss('body')
.
filter
((
x
)
=>
x
.
source
===
'error'
)
.
map
((
x
)
=>
x
.
message
)
if
(
isDev
)
{
.
join
(
'
\
n'
)
expect
(
await
hasRedbox
(
browser
)).
toBe
(
true
)
expect
(
errors
).
toInclude
(
'Error during SSR'
)
console
.
log
(
'getRedboxHeader'
,
await
getRedboxHeader
(
browser
))
// expect(await getRedboxHeader(browser)).toMatch(/An error occurred: this is a test/)
}
else
{
expect
(
await
browser
.
waitForElementByCss
(
'body'
)
.
elementByCss
(
'body'
)
.
text
()
).
toBe
(
'Application error: a client-side exception has occurred (see the browser console for more information).'
)
}
})
})
if
(
!
isDev
)
{
it
(
'should allow resetting error boundary'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/error/client-component'
)
// Try triggering and resetting a few times in a row
for
(
let
i
=
0
;
i
<
5
;
i
++
)
{
await
browser
.
elementByCss
(
'#error-trigger-button'
)
.
click
()
.
waitForElementByCss
(
'#error-boundary-message'
)
expect
(
await
browser
.
elementByCss
(
'#error-boundary-message'
).
text
()
).
toBe
(
'An error occurred: this is a test'
)
await
browser
.
elementByCss
(
'#reset'
)
.
click
()
.
waitForElementByCss
(
'#error-trigger-button'
)
expect
(
await
browser
.
elementByCss
(
'#error-trigger-button'
).
text
()
).
toBe
(
'Trigger Error!'
)
}
})
it
(
'should hydrate empty shell to handle server-side rendering errors'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/error/ssr-error-client-component'
)
const
logs
=
await
browser
.
log
()
const
errors
=
logs
.
filter
((
x
)
=>
x
.
source
===
'error'
)
.
map
((
x
)
=>
x
.
message
)
.
join
(
'
\
n'
)
expect
(
errors
).
toInclude
(
'Error during SSR'
)
})
}
})
})
describe
(
'known bugs'
,
()
=>
{
describe
(
'known bugs'
,
()
=>
{
...
...
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