Unverified Commit 47d7eddd authored by Thomas Heyenbrock's avatar Thomas Heyenbrock Committed by GitHub

fix(dev-server): render 404 when dynamic ssg path doesn't exist (#12085)

Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
Co-authored-by: 's avatarJoe Haddad <joe.haddad@zeit.co>
parent 5d870994
...@@ -513,7 +513,11 @@ export default class DevServer extends Server { ...@@ -513,7 +513,11 @@ export default class DevServer extends Server {
pathname: string, pathname: string,
query: { [key: string]: string } query: { [key: string]: string }
) { ) {
if (res.statusCode === 404 && (await this.hasPage('/404'))) {
await this.hotReloader!.ensurePage('/404')
} else {
await this.hotReloader!.ensurePage('/_error') await this.hotReloader!.ensurePage('/_error')
}
const compilationErr = await this.getCompilationError(pathname) const compilationErr = await this.getCompilationError(pathname)
if (compilationErr) { if (compilationErr) {
......
const page = () => 'custom 404 page'
export default page
export const getStaticPaths = async () => ({
paths: ['/post/1'],
fallback: false,
})
export const getStaticProps = async () => ({ props: {} })
export default () => 'blog post'
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import {
killApp,
findPort,
launchApp,
nextStart,
nextBuild,
fetchViaHTTP,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
const runTests = () => {
it('should respond to a not existing page with 404', async () => {
const res = await fetchViaHTTP(appPort, '/post/2')
expect(res.status).toBe(404)
expect(await res.text()).toContain('custom 404 page')
})
}
describe('Custom 404 Page for static site generation with dynamic routes', () => {
describe('server mode', () => {
afterAll(() => killApp(app))
it('should build successfully', async () => {
const { code } = await nextBuild(appDir, [], {
stderr: true,
stdout: true,
})
expect(code).toBe(0)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
runTests('server')
})
describe('serverless mode', () => {
afterAll(async () => {
await fs.remove(nextConfig)
await killApp(app)
})
it('should build successfully', async () => {
await fs.writeFile(
nextConfig,
`
module.exports = { target: 'experimental-serverless-trace' }
`
)
const { code } = await nextBuild(appDir, [], {
stderr: true,
stdout: true,
})
expect(code).toBe(0)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
runTests('serverless')
})
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests('dev')
})
})
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