Unverified Commit 7b524c1b authored by Shu Ding's avatar Shu Ding Committed by GitHub

Properly support custom 500 page in the web server (#33729)

In the web runtime, currently we use `absolute500Path || absoluteErrorPath` to act like `/_error`. This PR fixes the behavior to use the `/pages/500.js` for 500 errors and `/pages/_error.js` for 500 fallback and other errors.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 3bda6e66
...@@ -14,28 +14,26 @@ export default async function middlewareSSRLoader(this: any) { ...@@ -14,28 +14,26 @@ export default async function middlewareSSRLoader(this: any) {
stringifiedConfig, stringifiedConfig,
} = this.getOptions() } = this.getOptions()
const stringifiedAbsolutePagePath = stringifyRequest(this, absolutePagePath) const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const stringifiedAbsoluteAppPath = stringifyRequest(this, absoluteAppPath) const stringifiedAppPath = stringifyRequest(this, absoluteAppPath)
const stringifiedAbsolute500PagePath = stringifyRequest( const stringifiedErrorPath = stringifyRequest(this, absoluteErrorPath)
this, const stringifiedDocumentPath = stringifyRequest(this, absoluteDocumentPath)
absolute500Path || absoluteErrorPath const stringified500Path = absolute500Path
) ? stringifyRequest(this, absolute500Path)
const stringifiedAbsoluteDocumentPath = stringifyRequest( : 'null'
this,
absoluteDocumentPath
)
const transformed = ` const transformed = `
import { adapter } from 'next/dist/server/web/adapter' import { adapter } from 'next/dist/server/web/adapter'
import { RouterContext } from 'next/dist/shared/lib/router-context' 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' import { getRender } from 'next/dist/build/webpack/loaders/next-middleware-ssr-loader/render'
const pageMod = require(${stringifiedAbsolutePagePath}) import App from ${stringifiedAppPath}
const errorMod = require(${stringifiedAbsolute500PagePath}) import Document from ${stringifiedDocumentPath}
const pageMod = require(${stringifiedPagePath})
const errorMod = require(${stringifiedErrorPath})
const error500Mod = ${stringified500Path} ? require(${stringified500Path}) : null
const buildManifest = self.__BUILD_MANIFEST const buildManifest = self.__BUILD_MANIFEST
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
...@@ -62,6 +60,7 @@ export default async function middlewareSSRLoader(this: any) { ...@@ -62,6 +60,7 @@ export default async function middlewareSSRLoader(this: any) {
// components // components
errorMod, errorMod,
error500Mod,
// renderOpts // renderOpts
buildId: ${JSON.stringify(buildId)}, buildId: ${JSON.stringify(buildId)},
......
...@@ -169,8 +169,26 @@ export default class NextWebServer extends BaseServer { ...@@ -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') { if (pathname === '/_error') {
const errorMod = (globalThis as any).__server_context.errorMod
return { return {
query: { query: {
...(query || {}), ...(query || {}),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment