From 2ed6aa377c88d9b9b64a87ef7e96c71bddcfa5ae Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 14 May 2022 17:34:21 +0800 Subject: [PATCH 1/4] test(#36855): add test case --- test/integration/export-404-html/.gitignore | 1 + .../export-404-html/next.config.js | 5 +++ .../export-404-html/pages/index.js | 1 + .../export-404-html/test/index.test.js | 43 +++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 test/integration/export-404-html/.gitignore create mode 100644 test/integration/export-404-html/next.config.js create mode 100644 test/integration/export-404-html/pages/index.js create mode 100644 test/integration/export-404-html/test/index.test.js diff --git a/test/integration/export-404-html/.gitignore b/test/integration/export-404-html/.gitignore new file mode 100644 index 000000000000..3ec8dc5141d7 --- /dev/null +++ b/test/integration/export-404-html/.gitignore @@ -0,0 +1 @@ +.next-dev diff --git a/test/integration/export-404-html/next.config.js b/test/integration/export-404-html/next.config.js new file mode 100644 index 000000000000..d703fe4768a8 --- /dev/null +++ b/test/integration/export-404-html/next.config.js @@ -0,0 +1,5 @@ +module.exports = (phase) => { + return { + trailingSlash: false, + } +} diff --git a/test/integration/export-404-html/pages/index.js b/test/integration/export-404-html/pages/index.js new file mode 100644 index 000000000000..cdcba171398a --- /dev/null +++ b/test/integration/export-404-html/pages/index.js @@ -0,0 +1 @@ +export default () =>
diff --git a/test/integration/export-404-html/test/index.test.js b/test/integration/export-404-html/test/index.test.js new file mode 100644 index 000000000000..49ef5852a7d5 --- /dev/null +++ b/test/integration/export-404-html/test/index.test.js @@ -0,0 +1,43 @@ +/* eslint-env jest */ + +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) + }) +}) From e5f909aaffc7ad7d5ca6eec97d95ce1dbab50473 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 14 May 2022 20:12:17 +0800 Subject: [PATCH 2/4] fix(#36855/#30300): export 404.html correctly --- packages/next/export/index.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/next/export/index.ts b/packages/next/export/index.ts index 366a00e4e1ee..fd08ec9ab0b6 100644 --- a/packages/next/export/index.ts +++ b/packages/next/export/index.ts @@ -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'] } } From 08214a97e986d939737119ada7582a221cfb8b79 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 14 May 2022 20:44:37 +0800 Subject: [PATCH 3/4] chore/test: remove unnecessary .gitignore file --- test/integration/export-404-html/.gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test/integration/export-404-html/.gitignore diff --git a/test/integration/export-404-html/.gitignore b/test/integration/export-404-html/.gitignore deleted file mode 100644 index 3ec8dc5141d7..000000000000 --- a/test/integration/export-404-html/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.next-dev From de95c826e3541ed9a72685e7c514dd03b008f9ab Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 14 May 2022 21:37:21 +0800 Subject: [PATCH 4/4] test: merge export-404-html & export-override-404 --- .../export-404-html/pages/index.js | 1 - .../next.config.js | 0 .../pages/404.js | 0 .../test/index.test.js | 18 +++++++++++----- .../export-override-404/test/index.test.js | 21 ------------------- 5 files changed, 13 insertions(+), 27 deletions(-) delete mode 100644 test/integration/export-404-html/pages/index.js rename test/integration/{export-404-html => export-404}/next.config.js (100%) rename test/integration/{export-override-404 => export-404}/pages/404.js (100%) rename test/integration/{export-404-html => export-404}/test/index.test.js (77%) delete mode 100644 test/integration/export-override-404/test/index.test.js diff --git a/test/integration/export-404-html/pages/index.js b/test/integration/export-404-html/pages/index.js deleted file mode 100644 index cdcba171398a..000000000000 --- a/test/integration/export-404-html/pages/index.js +++ /dev/null @@ -1 +0,0 @@ -export default () =>
diff --git a/test/integration/export-404-html/next.config.js b/test/integration/export-404/next.config.js similarity index 100% rename from test/integration/export-404-html/next.config.js rename to test/integration/export-404/next.config.js diff --git a/test/integration/export-override-404/pages/404.js b/test/integration/export-404/pages/404.js similarity index 100% rename from test/integration/export-override-404/pages/404.js rename to test/integration/export-404/pages/404.js diff --git a/test/integration/export-404-html/test/index.test.js b/test/integration/export-404/test/index.test.js similarity index 77% rename from test/integration/export-404-html/test/index.test.js rename to test/integration/export-404/test/index.test.js index 49ef5852a7d5..b90a681e4336 100644 --- a/test/integration/export-404-html/test/index.test.js +++ b/test/integration/export-404/test/index.test.js @@ -1,15 +1,13 @@ /* eslint-env jest */ +import { promises } from 'fs' import { join } from 'path' import { nextBuild, nextExport, File } from 'next-test-utils' -import { promises } from 'fs' - -const { access, stat } = promises +const { readFile, 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) => @@ -41,3 +39,13 @@ describe('Static 404 Export', () => { expect(await fileExist(join(outdir, '404.html'))).toBe(true) }) }) + +describe('Export with a page named 404.js', () => { + it('should export a custom 404.html instead of default 404.html', async () => { + await nextBuild(appDir) + await nextExport(appDir, { outdir }) + + const html = await readFile(join(outdir, '404.html'), 'utf8') + expect(html).toMatch(/this is a 404 page override the default 404\.html/) + }) +}) diff --git a/test/integration/export-override-404/test/index.test.js b/test/integration/export-override-404/test/index.test.js deleted file mode 100644 index 9ce5134a901d..000000000000 --- a/test/integration/export-override-404/test/index.test.js +++ /dev/null @@ -1,21 +0,0 @@ -/* eslint-env jest */ - -import { promises } from 'fs' -import { join } from 'path' -import { nextBuild, nextExport } from 'next-test-utils' - -const { readFile } = promises -const appDir = join(__dirname, '../') -const outdir = join(appDir, 'out') - -describe('Export with a page named 404.js', () => { - beforeAll(async () => { - await nextBuild(appDir) - await nextExport(appDir, { outdir }) - }) - - it('should export a custom 404.html instead of default 404.html', async () => { - const html = await readFile(join(outdir, '404.html'), 'utf8') - expect(html).toMatch(/this is a 404 page override the default 404\.html/) - }) -})