1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Make sure 404 is rendered (#5880)

This commit is contained in:
Tim Neutkens 2018-12-13 19:46:16 +01:00 committed by GitHub
parent 1016b71011
commit 4426fdb98e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View file

@ -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 {
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})
} }

View file

@ -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', () => {
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)