Unverified Commit 4426fdb9 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Make sure 404 is rendered (#5880)

parent 1016b710
...@@ -231,6 +231,7 @@ export default class Server { ...@@ -231,6 +231,7 @@ export default class Server {
return html return html
} catch (err) { } catch (err) {
if (err.code === 'ENOENT') { if (err.code === 'ENOENT') {
res.statusCode = 404
return this.renderErrorToHTML(null, req, res, pathname, query) return this.renderErrorToHTML(null, req, res, pathname, query)
} else { } else {
if (!this.quiet) console.error(err) if (!this.quiet) console.error(err)
...@@ -249,7 +250,7 @@ export default class Server { ...@@ -249,7 +250,7 @@ export default class Server {
return this.sendHTML(req, res, html) return this.sendHTML(req, res, html)
} }
public async renderErrorToHTML (err: Error|null, req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery = {}) { public async renderErrorToHTML (err: Error|null, req: IncomingMessage, res: ServerResponse, _pathname: string, query: ParsedUrlQuery = {}) {
return renderToHTML(req, res, '/_error', query, {...this.renderOpts, err}) return renderToHTML(req, res, '/_error', query, {...this.renderOpts, err})
} }
......
...@@ -17,7 +17,7 @@ import dynamicImportTests from './dynamic' ...@@ -17,7 +17,7 @@ import dynamicImportTests from './dynamic'
import processEnv from './process-env' import processEnv from './process-env'
import security from './security' import security from './security'
import {BUILD_MANIFEST, REACT_LOADABLE_MANIFEST, PAGES_MANIFEST} from 'next-server/constants' import {BUILD_MANIFEST, REACT_LOADABLE_MANIFEST, PAGES_MANIFEST} from 'next-server/constants'
import cheerio from 'cheerio'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
let appPort let appPort
let server let server
...@@ -55,6 +55,16 @@ describe('Production Usage', () => { ...@@ -55,6 +55,16 @@ describe('Production Usage', () => {
expect(res2.status).toBe(304) expect(res2.status).toBe(304)
}) })
it('should render 404 for routes that do not exist', async () => {
const url = `http://localhost:${appPort}/abcdefghijklmno`
const res = await fetch(url)
const text = await res.text()
const $html = cheerio.load(text)
expect($html('html').text()).toMatch(/404/)
expect(text).toMatch(/"statusCode":404/)
expect(res.status).toBe(404)
})
it('should render 404 for _next routes that do not exist', async () => { it('should render 404 for _next routes that do not exist', async () => {
const url = `http://localhost:${appPort}/_next/abcdef` const url = `http://localhost:${appPort}/_next/abcdef`
const res = await fetch(url) const res = await fetch(url)
......
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