Skip to content

Commit 987dfb2

Browse files
committed
fix: Prevent infinite loop in _error page
1 parent cdab7bf commit 987dfb2

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/next/client/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import '@next/polyfill-module'
33
import React from 'react'
44
import ReactDOM from 'react-dom'
5+
import ErrorComponentBuiltIn from 'next/error'
56
import { HeadManagerContext } from '../next-server/lib/head-manager-context'
67
import mitt, { MittEmitter } from '../next-server/lib/mitt'
78
import { RouterContext } from '../next-server/lib/router-context'
@@ -472,12 +473,18 @@ export function renderError(renderErrorProps: RenderErrorProps): Promise<any> {
472473
return pageLoader
473474
.loadPage('/_error')
474475
.then(({ page: ErrorComponent, styleSheets }) => {
476+
// To prevent infinite `_error` rendering when `_error` throws on client side
477+
const Component =
478+
lastAppProps.Component === ErrorComponent
479+
? ErrorComponentBuiltIn
480+
: ErrorComponent
481+
475482
// In production we do a normal render with the `ErrorComponent` as component.
476483
// If we've gotten here upon initial render, we can use the props from the server.
477484
// Otherwise, we need to call `getInitialProps` on `App` before mounting.
478485
const AppTree = wrapApp(App)
479486
const appCtx = {
480-
Component: ErrorComponent,
487+
Component,
481488
AppTree,
482489
router,
483490
ctx: { err, pathname: page, query, asPath, AppTree },
@@ -490,7 +497,7 @@ export function renderError(renderErrorProps: RenderErrorProps): Promise<any> {
490497
doRender({
491498
...renderErrorProps,
492499
err,
493-
Component: ErrorComponent,
500+
Component,
494501
styleSheets,
495502
props: initProps,
496503
})

0 commit comments

Comments
 (0)