Skip to content

Commit 7b524c1

Browse files
authored
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`
1 parent 3bda6e6 commit 7b524c1

2 files changed

Lines changed: 33 additions & 16 deletions

File tree

packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,26 @@ export default async function middlewareSSRLoader(this: any) {
1414
stringifiedConfig,
1515
} = this.getOptions()
1616

17-
const stringifiedAbsolutePagePath = stringifyRequest(this, absolutePagePath)
18-
const stringifiedAbsoluteAppPath = stringifyRequest(this, absoluteAppPath)
19-
const stringifiedAbsolute500PagePath = stringifyRequest(
20-
this,
21-
absolute500Path || absoluteErrorPath
22-
)
23-
const stringifiedAbsoluteDocumentPath = stringifyRequest(
24-
this,
25-
absoluteDocumentPath
26-
)
17+
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
18+
const stringifiedAppPath = stringifyRequest(this, absoluteAppPath)
19+
const stringifiedErrorPath = stringifyRequest(this, absoluteErrorPath)
20+
const stringifiedDocumentPath = stringifyRequest(this, absoluteDocumentPath)
21+
const stringified500Path = absolute500Path
22+
? stringifyRequest(this, absolute500Path)
23+
: 'null'
2724

2825
const transformed = `
2926
import { adapter } from 'next/dist/server/web/adapter'
3027
import { RouterContext } from 'next/dist/shared/lib/router-context'
3128
32-
import App from ${stringifiedAbsoluteAppPath}
33-
import Document from ${stringifiedAbsoluteDocumentPath}
34-
3529
import { getRender } from 'next/dist/build/webpack/loaders/next-middleware-ssr-loader/render'
3630
37-
const pageMod = require(${stringifiedAbsolutePagePath})
38-
const errorMod = require(${stringifiedAbsolute500PagePath})
31+
import App from ${stringifiedAppPath}
32+
import Document from ${stringifiedDocumentPath}
33+
34+
const pageMod = require(${stringifiedPagePath})
35+
const errorMod = require(${stringifiedErrorPath})
36+
const error500Mod = ${stringified500Path} ? require(${stringified500Path}) : null
3937
4038
const buildManifest = self.__BUILD_MANIFEST
4139
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
@@ -62,6 +60,7 @@ export default async function middlewareSSRLoader(this: any) {
6260
6361
// components
6462
errorMod,
63+
error500Mod,
6564
6665
// renderOpts
6766
buildId: ${JSON.stringify(buildId)},

packages/next/server/web-server.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,26 @@ export default class NextWebServer extends BaseServer {
169169
}
170170
}
171171

172+
const { errorMod, error500Mod } = (globalThis as any).__server_context
173+
174+
// If there is a custom 500 page.
175+
if (pathname === '/500' && error500Mod) {
176+
return {
177+
query: {
178+
...(query || {}),
179+
...(params || {}),
180+
},
181+
components: {
182+
...(globalThis as any).__server_context,
183+
Component: error500Mod.default,
184+
getStaticProps: error500Mod.getStaticProps,
185+
getServerSideProps: error500Mod.getServerSideProps,
186+
getStaticPaths: error500Mod.getStaticPaths,
187+
} as LoadComponentsReturnType,
188+
}
189+
}
190+
172191
if (pathname === '/_error') {
173-
const errorMod = (globalThis as any).__server_context.errorMod
174192
return {
175193
query: {
176194
...(query || {}),

0 commit comments

Comments
 (0)