22import '@next/polyfill-module'
33import React from 'react'
44import ReactDOM from 'react-dom'
5+ import ErrorComponentBuiltIn from 'next/error'
56import { HeadManagerContext } from '../next-server/lib/head-manager-context'
67import mitt , { MittEmitter } from '../next-server/lib/mitt'
78import { 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