Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#36855/#30300): export 404.html correctly #36910

Merged
merged 4 commits into from May 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions packages/next/export/index.ts
Expand Up @@ -418,13 +418,20 @@ export default async function exportApp(
})
)

if (
!options.buildExport &&
!exportPathMap['/404'] &&
!exportPathMap['/404.html']
) {
exportPathMap['/404'] = exportPathMap['/404.html'] = {
page: '/_error',
// only add missing 404 page when `buildExport` is false
if (!options.buildExport) {
// only add missing /404 if not specified in `exportPathMap`
if (!exportPathMap['/404']) {
exportPathMap['/404'] = { page: '/_error' }
}

/**
* exports 404.html for backwards compat
* E.g. GitHub Pages, GitLab Pages, Cloudflare Pages, Netlify
*/
if (!exportPathMap['/404.html']) {
// alias /404.html to /404 to be compatible with custom 404 / _error page
exportPathMap['/404.html'] = exportPathMap['/404']
}
}

Expand Down
1 change: 1 addition & 0 deletions test/integration/export-404-html/.gitignore
@@ -0,0 +1 @@
.next-dev
SukkaW marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions test/integration/export-404-html/next.config.js
@@ -0,0 +1,5 @@
module.exports = (phase) => {
return {
trailingSlash: false,
}
}
1 change: 1 addition & 0 deletions test/integration/export-404-html/pages/index.js
@@ -0,0 +1 @@
export default () => <div id="home-page" />
43 changes: 43 additions & 0 deletions test/integration/export-404-html/test/index.test.js
@@ -0,0 +1,43 @@
/* eslint-env jest */
huozhi marked this conversation as resolved.
Show resolved Hide resolved

import { join } from 'path'
import { nextBuild, nextExport, File } from 'next-test-utils'

import { promises } from 'fs'

const { access, stat } = promises
const appDir = join(__dirname, '../')
const outdir = join(appDir, 'out')
const context = {}
context.appDir = appDir
const nextConfig = new File(join(appDir, 'next.config.js'))

const fileExist = (path) =>
access(path)
.then(() => stat(path))
.then((stats) => (stats.isFile() ? true : false))
.catch(() => false)

// Issue #36855
// https://github.com/vercel/next.js/issues/36855
describe('Static 404 Export', () => {
it('only export 404.html when trailingSlash: false', async () => {
await nextBuild(appDir)
await nextExport(appDir, { outdir })

expect(await fileExist(join(outdir, '404.html'))).toBe(true)
expect(await fileExist(join(outdir, '404.html.html'))).toBe(false)
expect(await fileExist(join(outdir, '404/index.html'))).toBe(false)
})

it('export 404.html and 404/index.html when trailingSlash: true', async () => {
nextConfig.replace(`trailingSlash: false`, `trailingSlash: true`)
await nextBuild(appDir)
await nextExport(appDir, { outdir })
nextConfig.restore()

expect(await fileExist(join(outdir, '404/index.html'))).toBe(true)
expect(await fileExist(join(outdir, '404.html.html'))).toBe(false)
expect(await fileExist(join(outdir, '404.html'))).toBe(true)
})
})