From cade8c84c44928c4162fc2784249461a94e3085d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Fri, 16 Sep 2022 23:39:48 +0200 Subject: [PATCH] fix: handle `notFound: true` in `/` with `next export` (#40592) Closes #40571 An earlier fix in #24481 did not consider the `/` case. The page path normalization method `normalizePagePath` turned `/` into `/index` and the route matching was skipped for the index route's non-existent HTML file. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) --- packages/next/export/index.ts | 2 +- .../export-index-not-found-gsp/pages/index.js | 7 +++++++ .../test/index.test.ts | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/integration/export-index-not-found-gsp/pages/index.js create mode 100644 test/integration/export-index-not-found-gsp/test/index.test.ts diff --git a/packages/next/export/index.ts b/packages/next/export/index.ts index b1612ece4853294..375d4f915571145 100644 --- a/packages/next/export/index.ts +++ b/packages/next/export/index.ts @@ -640,13 +640,13 @@ export default async function exportApp( Object.keys(prerenderManifest.routes).map(async (route) => { const { srcRoute } = prerenderManifest!.routes[route] const pageName = srcRoute || route - route = normalizePagePath(route) // returning notFound: true from getStaticProps will not // output html/json files during the build if (prerenderManifest!.notFoundRoutes.includes(route)) { return } + route = normalizePagePath(route) const pagePath = getPagePath(pageName, distDir, isLikeServerless) const distPagesDir = join( diff --git a/test/integration/export-index-not-found-gsp/pages/index.js b/test/integration/export-index-not-found-gsp/pages/index.js new file mode 100644 index 000000000000000..f5ec69c7fffa704 --- /dev/null +++ b/test/integration/export-index-not-found-gsp/pages/index.js @@ -0,0 +1,7 @@ +export default function Page() { + return 'Hello world' +} + +export function getStaticProps() { + return { notFound: true } +} diff --git a/test/integration/export-index-not-found-gsp/test/index.test.ts b/test/integration/export-index-not-found-gsp/test/index.test.ts new file mode 100644 index 000000000000000..c915d7af1b8797e --- /dev/null +++ b/test/integration/export-index-not-found-gsp/test/index.test.ts @@ -0,0 +1,21 @@ +/* eslint-env jest */ + +import fs from 'fs-extra' +import { join } from 'path' +import { nextBuild, nextExport } from 'next-test-utils' + +const appDir = join(__dirname, '../') +const outdir = join(appDir, 'out') + +describe('Export index page with `notFound: true` in `getStaticProps`', () => { + it('should build successfully', async () => { + await fs.remove(join(appDir, '.next')) + const { code } = await nextBuild(appDir) + if (code !== 0) throw new Error(`build failed with status ${code}`) + }) + + it('should export successfully', async () => { + const { code } = await nextExport(appDir, { outdir }) + if (code !== 0) throw new Error(`export failed with status ${code}`) + }) +})