Skip to content

Commit

Permalink
fix(dev-server): render 404 when dynamic ssg path doesn't exist (#12085)
Browse files Browse the repository at this point in the history
Co-authored-by: JJ Kasper <[email protected]>
Co-authored-by: Joe Haddad <[email protected]>
  • Loading branch information
3 people authored Apr 22, 2020
1 parent 5d87099 commit 47d7edd
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/next/server/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,11 @@ export default class DevServer extends Server {
pathname: string,
query: { [key: string]: string }
) {
await this.hotReloader!.ensurePage('/_error')
if (res.statusCode === 404 && (await this.hasPage('/404'))) {
await this.hotReloader!.ensurePage('/404')
} else {
await this.hotReloader!.ensurePage('/_error')
}

const compilationErr = await this.getCompilationError(pathname)
if (compilationErr) {
Expand Down
2 changes: 2 additions & 0 deletions test/integration/ssg-dynamic-routes-404-page/pages/404.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const page = () => 'custom 404 page'
export default page
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const getStaticPaths = async () => ({
paths: ['/post/1'],
fallback: false,
})
export const getStaticProps = async () => ({ props: {} })
export default () => 'blog post'
86 changes: 86 additions & 0 deletions test/integration/ssg-dynamic-routes-404-page/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* 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')
})
})

0 comments on commit 47d7edd

Please sign in to comment.