Skip to content

Commit d01b074

Browse files
committed
use require.ensure for dynamic import
1 parent e2cc5fc commit d01b074

File tree

6 files changed

+27
-24
lines changed

6 files changed

+27
-24
lines changed

packages/next/client/index.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ export default async (opts: { webpackHMR?: any } = {}) => {
415415
}
416416
}
417417

418+
function requireEnsureAsync(dep: string): Promise<any> {
419+
return new Promise((resolve, reject) => {
420+
return require.ensure([dep], (require) => resolve(require(dep)), reject)
421+
})
422+
}
423+
418424
export async function render(renderingProps: RenderRouteInfo): Promise<void> {
419425
if (renderingProps.err) {
420426
await renderError(renderingProps)
@@ -466,13 +472,17 @@ export function renderError(renderErrorProps: RenderErrorProps): Promise<any> {
466472
console.error(err)
467473
return pageLoader
468474
.loadPage('/_error')
469-
.then(({ page: ErrorComponent, styleSheets }) => {
470-
return lastAppProps?.Component === ErrorComponent
471-
? import('next/dist/pages/_error').then((m) => ({
472-
ErrorComponent: m.default as React.ComponentType<{}>,
473-
styleSheets: [],
474-
}))
475-
: { ErrorComponent, styleSheets }
475+
.then(async ({ page: ErrorComponent, styleSheets }) => {
476+
if (lastAppProps?.Component === ErrorComponent) {
477+
// Error loop detected on rerender
478+
const DefaultError = (await requireEnsureAsync('../pages/_error'))
479+
.default as React.ComponentType<{}>
480+
return {
481+
ErrorComponent: DefaultError,
482+
styleSheets: [],
483+
}
484+
}
485+
return { ErrorComponent, styleSheets }
476486
})
477487
.then(({ ErrorComponent, styleSheets }) => {
478488
// In production we do a normal render with the `ErrorComponent` as component.

packages/next/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
"@types/styled-jsx": "2.2.8",
184184
"@types/text-table": "0.2.1",
185185
"@types/webpack": "5.28.0",
186+
"@types/webpack-env": "1.16.0",
186187
"@types/webpack-sources": "0.1.5",
187188
"@vercel/ncc": "0.27.0",
188189
"amphtml-validator": "1.0.33",

packages/next/taskfile-babel.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const path = require('path')
66
// eslint-disable-next-line import/no-extraneous-dependencies
77
const transform = require('@babel/core').transform
88

9-
const clientPath = path.resolve(__dirname, 'client')
10-
119
const babelClientPresetEnvOptions = {
1210
modules: 'commonjs',
1311
targets: {
@@ -41,15 +39,6 @@ const babelClientOpts = {
4139
// eslint-disable-next-line import/no-extraneous-dependencies
4240
plugins: [require('@babel/plugin-proposal-numeric-separator').default],
4341
},
44-
{
45-
test: (p) => p.startsWith(clientPath),
46-
presets: [
47-
[
48-
'@babel/preset-env',
49-
{ ...babelClientPresetEnvOptions, modules: false },
50-
],
51-
],
52-
},
5342
],
5443
}
5544

packages/next/types/misc.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ declare module 'next/dist/compiled/arg/index.js' {
4545
export = arg
4646
}
4747

48-
declare module 'next/dist/pages/_error'
49-
5048
declare module 'next/dist/compiled/babel/code-frame' {
5149
export * from '@babel/code-frame'
5250
}

test/integration/build-output/test/index.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ describe('Build Output', () => {
123123
)
124124
expect(indexSize.endsWith('B')).toBe(true)
125125

126-
expect(parseFloat(indexFirstLoad)).toBeCloseTo(gz ? 64 : 196, 1)
126+
expect(parseFloat(indexFirstLoad)).toBeCloseTo(gz ? 64.2 : 197, 1)
127127
expect(indexFirstLoad.endsWith('kB')).toBe(true)
128128

129129
expect(parseFloat(err404Size)).toBeCloseTo(gz ? 3.17 : 8.51, 1)
130130
expect(err404Size.endsWith('kB')).toBe(true)
131131

132-
expect(parseFloat(err404FirstLoad)).toBeCloseTo(gz ? 66.9 : 205, 1)
132+
expect(parseFloat(err404FirstLoad)).toBeCloseTo(gz ? 67.1 : 205, 1)
133133
expect(err404FirstLoad.endsWith('kB')).toBe(true)
134134

135-
expect(parseFloat(sharedByAll)).toBeCloseTo(gz ? 63.7 : 196, 1)
135+
expect(parseFloat(sharedByAll)).toBeCloseTo(gz ? 63.9 : 197, 1)
136136
expect(sharedByAll.endsWith('kB')).toBe(true)
137137

138138
const appSizeValue = _appSize.endsWith('kB')
@@ -149,7 +149,7 @@ describe('Build Output', () => {
149149
true
150150
)
151151

152-
expect(parseFloat(mainSize)).toBeCloseTo(gz ? 20.1 : 62.7, 1)
152+
expect(parseFloat(mainSize)).toBeCloseTo(gz ? 20.3 : 63.5, 1)
153153
expect(mainSize.endsWith('kB')).toBe(true)
154154

155155
expect(parseFloat(frameworkSize)).toBeCloseTo(gz ? 42.0 : 130, 1)

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4267,6 +4267,11 @@
42674267
"@types/unist" "*"
42684268
"@types/vfile-message" "*"
42694269

4270+
"@types/webpack-env@1.16.0":
4271+
version "1.16.0"
4272+
resolved "https://registry.yarnpkg.com/@types/webpack-env/-/webpack-env-1.16.0.tgz#8c0a9435dfa7b3b1be76562f3070efb3f92637b4"
4273+
integrity sha512-Fx+NpfOO0CpeYX2g9bkvX8O5qh9wrU1sOF4g8sft4Mu7z+qfe387YlyY8w8daDyDsKY5vUxM0yxkAYnbkRbZEw==
4274+
42704275
"@types/webpack-sources@0.1.5":
42714276
version "0.1.5"
42724277
resolved "https://registry.yarnpkg.com/@types/webpack-sources/-/webpack-sources-0.1.5.tgz#be47c10f783d3d6efe1471ff7f042611bd464a92"

0 commit comments

Comments
 (0)