Unverified Commit 5fff528e authored by Steven's avatar Steven Committed by GitHub

fix: `not-found.tsx` with `output: export` (#52526)

Closes #48227 
Closes NEXT-1392

---------
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 67310755
export default function NotFound() {
return <h1>My custom not found page</h1>
}
......@@ -4,7 +4,7 @@ import { join } from 'path'
import {
appDir,
distDir,
expectedFiles,
expectedWhenTrailingSlashTrue,
exportDir,
getFiles,
nextConfig,
......@@ -36,7 +36,7 @@ describe('app dir with output export (next dev / next build)', () => {
await fs.remove(distDir)
await fs.remove(exportDir)
await nextBuild(appDir)
expect(await getFiles()).toEqual(expectedFiles)
expect(await getFiles()).toEqual(expectedWhenTrailingSlashTrue)
let stdout = ''
let stderr = ''
await nextExportDefault(appDir, {
......@@ -51,13 +51,13 @@ describe('app dir with output export (next dev / next build)', () => {
'- warn "next export" is no longer needed when "output: export" is configured in next.config.js'
)
expect(stdout).toContain('Export successful. Files written to')
expect(await getFiles()).toEqual(expectedFiles)
expect(await getFiles()).toEqual(expectedWhenTrailingSlashTrue)
})
it('should error when "next export -o <dir>" is used with config', async () => {
await fs.remove(distDir)
await fs.remove(exportDir)
await nextBuild(appDir)
expect(await getFiles()).toEqual(expectedFiles)
expect(await getFiles()).toEqual(expectedWhenTrailingSlashTrue)
let stdout = ''
let stderr = ''
let error = undefined
......@@ -132,7 +132,7 @@ describe('app dir with output export (next dev / next build)', () => {
)
try {
await nextBuild(appDir)
expect(await getFiles(outputDir)).toEqual(expectedFiles)
expect(await getFiles(outputDir)).toEqual(expectedWhenTrailingSlashTrue)
} finally {
nextConfig.restore()
await fs.remove(distDir)
......
......@@ -27,7 +27,7 @@ export const nextConfig = new File(join(appDir, 'next.config.js'))
const slugPage = new File(join(appDir, 'app/another/[slug]/page.js'))
const apiJson = new File(join(appDir, 'app/api/json/route.js'))
export const expectedFiles = [
export const expectedWhenTrailingSlashTrue = [
'404.html',
'404/index.html',
'_next/static/media/test.3f1a293b.png',
......@@ -49,6 +49,27 @@ export const expectedFiles = [
'robots.txt',
]
const expectedWhenTrailingSlashFalse = [
'404.html',
'_next/static/media/test.3f1a293b.png',
'_next/static/test-build-id/_buildManifest.js',
'_next/static/test-build-id/_ssgManifest.js',
'another.html',
'another.txt',
'another/first.html',
'another/first.txt',
'another/second.html',
'another/second.txt',
'api/json',
'api/txt',
'favicon.ico',
'image-import.html',
'image-import.txt',
'index.html',
'index.txt',
'robots.txt',
]
export async function getFiles(cwd = exportDir) {
const opts = { cwd, nodir: true }
const files = ((await glob('**/*', opts)) as string[])
......@@ -74,19 +95,19 @@ export async function runTests({
dynamicApiRoute?: string
expectedErrMsg?: string
}) {
if (trailingSlash) {
if (trailingSlash !== undefined) {
nextConfig.replace(
'trailingSlash: true,',
`trailingSlash: ${trailingSlash},`
)
}
if (dynamicPage) {
if (dynamicPage !== undefined) {
slugPage.replace(
`const dynamic = 'force-static'`,
`const dynamic = ${dynamicPage}`
)
}
if (dynamicApiRoute) {
if (dynamicApiRoute !== undefined) {
apiJson.replace(
`const dynamic = 'force-static'`,
`const dynamic = ${dynamicApiRoute}`
......@@ -156,7 +177,6 @@ export async function runTests({
await check(() => browser.elementByCss('h1').text(), 'Home')
expect(await browser.elementByCss(a(3)).text()).toBe('another first page')
await browser.elementByCss(a(3)).click()
await check(() => browser.elementByCss('h1').text(), 'first')
expect(await browser.elementByCss(a(1)).text()).toBe('Visit another page')
await browser.elementByCss(a(1)).click()
......@@ -188,8 +208,14 @@ export async function runTests({
expect(res2.status).toBe(200)
expect(await res2.text()).toEqual('this is plain text')
if (!isDev && trailingSlash) {
expect(await getFiles()).toEqual(expectedFiles)
if (!isDev) {
if (trailingSlash) {
expect(await getFiles()).toEqual(expectedWhenTrailingSlashTrue)
} else {
expect(await getFiles()).toEqual(expectedWhenTrailingSlashFalse)
}
const html404 = await fs.readFile(join(exportDir, '404.html'), 'utf8')
expect(html404).toContain('<h1>My custom not found page</h1>')
}
}
} finally {
......
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