From 4c94a858eb9698e5d989a94b3883877c64beca58 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 12 Nov 2021 01:47:40 +0100 Subject: [PATCH] 500 page --- packages/next/build/entries.ts | 2 +- .../next-middleware-ssr-loader/index.ts | 20 ++++++++++++------- .../app/pages/err.js | 5 +++++ .../test/index.test.js | 2 ++ 4 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 test/integration/react-streaming-and-server-components/app/pages/err.js diff --git a/packages/next/build/entries.ts b/packages/next/build/entries.ts index 42492d511d85354..a4b97bc00bd4544 100644 --- a/packages/next/build/entries.ts +++ b/packages/next/build/entries.ts @@ -162,7 +162,7 @@ export function createEntrypoints( page, absoluteAppPath: pages['/_app'], absoluteDocumentPath: pages['/_document'], - absoluteErrorPath: pages['/500'] || pages['/_error'], + absoluteErrorPath: pages['/_error'], absolutePagePath, isServerComponent: isFlight, buildId, diff --git a/packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts b/packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts index 810569b6ecd63bf..8f2144e22e4b987 100644 --- a/packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts +++ b/packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts @@ -16,15 +16,12 @@ export default async function middlewareRSCLoader(this: any) { const stringifiedAbsolutePagePath = stringifyRequest(this, absolutePagePath) const stringifiedAbsoluteAppPath = stringifyRequest(this, absoluteAppPath) const stringifiedAbsoluteErrorPath = stringifyRequest(this, absoluteErrorPath) + const stringified500PagePath = stringifyRequest(this, './pages/500') const stringifiedAbsoluteDocumentPath = stringifyRequest( this, absoluteDocumentPath ) - const appDefinition = `const App = require(${stringifiedAbsoluteAppPath}).default` - const documentDefinition = `const Document = require(${stringifiedAbsoluteDocumentPath}).default` - const errorDefinition = `const ErrorPage = require(${stringifiedAbsoluteErrorPath}).default` - const transformed = ` import { adapter } from 'next/dist/server/web/adapter' @@ -41,9 +38,18 @@ export default async function middlewareRSCLoader(this: any) { : '' } - ${appDefinition} - ${documentDefinition} - ${errorDefinition} + import App from ${stringifiedAbsoluteAppPath} + import Document from ${stringifiedAbsoluteDocumentPath} + + + let ErrorPage + try { + ErrorPage = require(${stringified500PagePath}).default + } catch (_) { + ErrorPage = require(${stringifiedAbsoluteErrorPath}).default + } + + // import ErrorPage from ${stringifiedAbsoluteErrorPath} const { default: Page, diff --git a/test/integration/react-streaming-and-server-components/app/pages/err.js b/test/integration/react-streaming-and-server-components/app/pages/err.js new file mode 100644 index 000000000000000..61c7136f7ba7e48 --- /dev/null +++ b/test/integration/react-streaming-and-server-components/app/pages/err.js @@ -0,0 +1,5 @@ +const page = () => 'page with err' +page.getInitialProps = () => { + throw new Error('oops') +} +export default page diff --git a/test/integration/react-streaming-and-server-components/test/index.test.js b/test/integration/react-streaming-and-server-components/test/index.test.js index d0d26c4671a1867..5bd7d097e78b36e 100644 --- a/test/integration/react-streaming-and-server-components/test/index.test.js +++ b/test/integration/react-streaming-and-server-components/test/index.test.js @@ -217,6 +217,7 @@ async function runBasicTests(context) { ) const path404HTML = await renderViaHTTP(context.appPort, '/404') + const path500HTML = await renderViaHTTP(context.appPort, '/err') const pathNotFoundHTML = await renderViaHTTP( context.appPort, '/this-is-not-found' @@ -230,6 +231,7 @@ async function runBasicTests(context) { expect(dynamicRouteHTML2).toContain('[pid]') expect(path404HTML).toContain('custom-404-page') + expect(path500HTML).toContain('custom-500-page') expect(pathNotFoundHTML).toContain('custom-404-page') })