Skip to content

Commit

Permalink
Make sure to only use 404 page for 404 error
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jan 30, 2020
1 parent 39c385e commit 499cd63
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,28 +1123,31 @@ export default class Server {
let result: null | LoadComponentsReturnType = null

const { static404, pages404 } = this.nextConfig.experimental
const is404 = res.statusCode === 404
let using404Page = false

// use static 404 page if available and is 404 response
if (static404 && res.statusCode === 404) {
try {
result = await this.findPageComponents('/_errors/404')
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
if (is404) {
if (static404) {
try {
result = await this.findPageComponents('/_errors/404')
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
}
}

// use 404 if /_errors/404 isn't available which occurs
// during development and when _app has getInitialProps
if (!result && pages404) {
try {
result = await this.findPageComponents('/404')
using404Page = true
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
// use 404 if /_errors/404 isn't available which occurs
// during development and when _app has getInitialProps
if (!result && pages404) {
try {
result = await this.findPageComponents('/404')
using404Page = true
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/integration/404-page/pages/err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const page = () => 'custom 404 page'
page.getInitialProps = () => {
throw new Error('oops')
}
export default page
6 changes: 6 additions & 0 deletions test/integration/404-page/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ const runTests = (mode = 'server') => {
expect(await res.text()).toContain('custom 404 page')
})

it('should render _error for a 500 error still', async () => {
const html = await renderViaHTTP(appPort, '/err')
expect(html).not.toContain('custom 404 page')
expect(html).toContain(mode === 'dev' ? 'oops' : 'Internal Server Error')
})

if (mode !== 'dev') {
it('should output _errors/404.html during build', async () => {
expect(
Expand Down

0 comments on commit 499cd63

Please sign in to comment.