From 73969381455a5e062c0db0113029e2ca749a8be8 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Thu, 27 Jan 2022 13:11:13 +0100 Subject: [PATCH 1/2] proper support for custom 500 page --- .../next-middleware-ssr-loader/index.ts | 29 +++++++++---------- packages/next/server/web-server.ts | 20 ++++++++++++- 2 files changed, 33 insertions(+), 16 deletions(-) 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 4f23d19936fcaaa..ff8ca8e8743398d 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 @@ -14,28 +14,26 @@ export default async function middlewareSSRLoader(this: any) { stringifiedConfig, } = this.getOptions() - const stringifiedAbsolutePagePath = stringifyRequest(this, absolutePagePath) - const stringifiedAbsoluteAppPath = stringifyRequest(this, absoluteAppPath) - const stringifiedAbsolute500PagePath = stringifyRequest( - this, - absolute500Path || absoluteErrorPath - ) - const stringifiedAbsoluteDocumentPath = stringifyRequest( - this, - absoluteDocumentPath - ) + const stringifiedPagePath = stringifyRequest(this, absolutePagePath) + const stringifiedAppPath = stringifyRequest(this, absoluteAppPath) + const stringifiedErrorPath = stringifyRequest(this, absoluteErrorPath) + const stringifiedDocumentPath = stringifyRequest(this, absoluteDocumentPath) + const stringified500Path = absolute500Path + ? stringifyRequest(this, absolute500Path) + : '' const transformed = ` import { adapter } from 'next/dist/server/web/adapter' import { RouterContext } from 'next/dist/shared/lib/router-context' - import App from ${stringifiedAbsoluteAppPath} - import Document from ${stringifiedAbsoluteDocumentPath} - import { getRender } from 'next/dist/build/webpack/loaders/next-middleware-ssr-loader/render' - const pageMod = require(${stringifiedAbsolutePagePath}) - const errorMod = require(${stringifiedAbsolute500PagePath}) + import App from ${stringifiedAppPath} + import Document from ${stringifiedDocumentPath} + + const pageMod = require(${stringifiedPagePath}) + const errorMod = require(${stringifiedErrorPath}) + const error500Mod = ${stringified500Path} ? require(${stringified500Path}) : null const buildManifest = self.__BUILD_MANIFEST const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST @@ -62,6 +60,7 @@ export default async function middlewareSSRLoader(this: any) { // components errorMod, + error500Mod, // renderOpts buildId: ${JSON.stringify(buildId)}, diff --git a/packages/next/server/web-server.ts b/packages/next/server/web-server.ts index 8d63d380b67d260..501d33f5aaaa01f 100644 --- a/packages/next/server/web-server.ts +++ b/packages/next/server/web-server.ts @@ -169,8 +169,26 @@ export default class NextWebServer extends BaseServer { } } + const { errorMod, error500Mod } = (globalThis as any).__server_context + + // If there is a custom 500 page. + if (pathname === '/500' && error500Mod) { + return { + query: { + ...(query || {}), + ...(params || {}), + }, + components: { + ...(globalThis as any).__server_context, + Component: error500Mod.default, + getStaticProps: error500Mod.getStaticProps, + getServerSideProps: error500Mod.getServerSideProps, + getStaticPaths: error500Mod.getStaticPaths, + } as LoadComponentsReturnType, + } + } + if (pathname === '/_error') { - const errorMod = (globalThis as any).__server_context.errorMod return { query: { ...(query || {}), From 71d59d53d79aa1d1d4a34bba94b96dd5106a16f2 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Thu, 27 Jan 2022 13:26:06 +0100 Subject: [PATCH 2/2] fix invalid token --- .../build/webpack/loaders/next-middleware-ssr-loader/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ff8ca8e8743398d..5341b620aba8b25 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 @@ -20,7 +20,7 @@ export default async function middlewareSSRLoader(this: any) { const stringifiedDocumentPath = stringifyRequest(this, absoluteDocumentPath) const stringified500Path = absolute500Path ? stringifyRequest(this, absolute500Path) - : '' + : 'null' const transformed = ` import { adapter } from 'next/dist/server/web/adapter'