Skip to content

Commit

Permalink
500 page
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Nov 12, 2021
1 parent 35968da commit 4c94a85
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/next/build/entries.ts
Expand Up @@ -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,
Expand Down
Expand Up @@ -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'
Expand All @@ -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,
Expand Down
@@ -0,0 +1,5 @@
const page = () => 'page with err'
page.getInitialProps = () => {
throw new Error('oops')
}
export default page
Expand Up @@ -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'
Expand All @@ -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')
})

Expand Down

0 comments on commit 4c94a85

Please sign in to comment.