Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/next/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ export function renderError(renderErrorProps: RenderErrorProps): Promise<any> {
return pageLoader
.loadPage('/_error')
.then(({ page: ErrorComponent, styleSheets }) => {
return lastAppProps?.Component === ErrorComponent
? import('../pages/_error').then((m) => ({
ErrorComponent: m.default as React.ComponentType<{}>,
styleSheets: [],
}))
: { ErrorComponent, styleSheets }
})
.then(({ ErrorComponent, styleSheets }) => {
// In production we do a normal render with the `ErrorComponent` as component.
// If we've gotten here upon initial render, we can use the props from the server.
// Otherwise, we need to call `getInitialProps` on `App` before mounting.
Expand Down
34 changes: 17 additions & 17 deletions packages/next/taskfile-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ const path = require('path')
// eslint-disable-next-line import/no-extraneous-dependencies
const transform = require('@babel/core').transform

const babelClientPresetEnvOptions = {
modules: 'commonjs',
targets: {
esmodules: true,
},
bugfixes: true,
loose: true,
// This is handled by the Next.js webpack config that will run next/babel over the same code.
exclude: [
'transform-typeof-symbol',
'transform-async-to-generator',
'transform-spread',
'proposal-dynamic-import',
],
}

const babelClientOpts = {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
modules: 'commonjs',
targets: {
esmodules: true,
},
bugfixes: true,
loose: true,
// This is handled by the Next.js webpack config that will run next/babel over the same code.
exclude: [
'transform-typeof-symbol',
'transform-async-to-generator',
'transform-spread',
],
},
],
['@babel/preset-env', babelClientPresetEnvOptions],
['@babel/preset-react', { useBuiltIns: true }],
],
plugins: [
Expand Down
12 changes: 12 additions & 0 deletions test/integration/custom-error-page-exception/pages/_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable no-unused-expressions, no-undef */
let renderCount = 0

export default function Error() {
renderCount++

// Guard to avoid endless loop crashing the browser tab.
if (typeof window !== 'undefined' && renderCount < 3) {
throw new Error('crash')
}
return `error threw ${renderCount} times`
}
20 changes: 20 additions & 0 deletions test/integration/custom-error-page-exception/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable no-unused-expressions, no-unused-vars */
import React from 'react'
import Link from 'next/link'

function page() {
return (
<Link href="/">
<a id="nav">Client side nav</a>
</Link>
)
}

page.getInitialProps = () => {
if (typeof window !== 'undefined') {
throw new Error('Oops from Home')
}
return {}
}

export default page
26 changes: 26 additions & 0 deletions test/integration/custom-error-page-exception/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-env jest */

import { join } from 'path'
import webdriver from 'next-webdriver'
import { nextBuild, nextStart, findPort, killApp } from 'next-test-utils'

jest.setTimeout(1000 * 60 * 1)

const appDir = join(__dirname, '..')
const navSel = '#nav'
const errorMessage = 'Application error: a client-side exception has occurred'

describe('Custom error page exception', () => {
it('should handle errors from _error render', async () => {
const { code } = await nextBuild(appDir)
const appPort = await findPort()
const app = await nextStart(appDir, appPort)
const browser = await webdriver(appPort, '/')
await browser.waitForElementByCss(navSel).elementByCss(navSel).click()
const text = await (await browser.elementByCss('#__next')).text()
killApp(app)

expect(code).toBe(0)
expect(text).toMatch(errorMessage)
})
})