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(#30300): force export 404.html #36827

Merged
merged 3 commits into from May 11, 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
7 changes: 6 additions & 1 deletion packages/next/export/worker.ts
Expand Up @@ -221,8 +221,13 @@ export default async function exportPage({
// extension of `.slug]`
const pageExt = isDynamic ? '' : extname(page)
const pathExt = isDynamic ? '' : extname(path)

// force output 404.html for backwards compat
if (path === '/404.html') {
htmlFilename = path
}
// Make sure page isn't a folder with a dot in the name e.g. `v1.2`
if (pageExt !== pathExt && pathExt !== '') {
else if (pageExt !== pathExt && pathExt !== '') {
const isBuiltinPaths = ['/500', '/404'].some(
(p) => p === path || p === path + '.html'
)
Expand Down
62 changes: 18 additions & 44 deletions test/integration/export/test/index.test.js
Expand Up @@ -20,7 +20,7 @@ import { promises } from 'fs'
import dynamic from './dynamic'
import apiRoutes from './api-routes'

const { access, mkdir, writeFile } = promises
const { access, mkdir, writeFile, stat } = promises
const appDir = join(__dirname, '../')
const outdir = join(appDir, 'out')
const outNoTrailSlash = join(appDir, 'outNoTrailSlash')
Expand All @@ -29,6 +29,12 @@ context.appDir = appDir
const devContext = {}
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)

describe('Static Export', () => {
it('should delete existing exported files', async () => {
const tempfile = join(outdir, 'temp.txt')
Expand Down Expand Up @@ -87,60 +93,28 @@ describe('Static Export', () => {
})

it('should honor exportTrailingSlash for 404 page', async () => {
expect(
await access(join(outdir, '404/index.html'))
.then(() => true)
.catch(() => false)
).toBe(true)
expect(await fileExist(join(outdir, '404/index.html'))).toBe(true)

// we still output 404.html for backwards compat
expect(
await access(join(outdir, '404.html'))
.then(() => true)
.catch(() => false)
).toBe(true)
expect(await fileExist(join(outdir, '404.html'))).toBe(true)
})

it('should handle trailing slash in getStaticPaths', async () => {
expect(
await access(join(outdir, 'gssp/foo/index.html'))
.then(() => true)
.catch(() => false)
).toBe(true)

expect(
await access(join(outNoTrailSlash, 'gssp/foo.html'))
.then(() => true)
.catch(() => false)
).toBe(true)
expect(await fileExist(join(outdir, 'gssp/foo/index.html'))).toBe(true)

expect(await fileExist(join(outNoTrailSlash, 'gssp/foo.html'))).toBe(true)
})

it('should only output 404.html without exportTrailingSlash', async () => {
expect(
await access(join(outNoTrailSlash, '404/index.html'))
.then(() => true)
.catch(() => false)
).toBe(false)

expect(
await access(join(outNoTrailSlash, '404.html'))
.then(() => true)
.catch(() => false)
).toBe(true)
expect(await fileExist(join(outNoTrailSlash, '404/index.html'))).toBe(false)

expect(await fileExist(join(outNoTrailSlash, '404.html'))).toBe(true)
})

it('should not duplicate /index with exportTrailingSlash', async () => {
expect(
await access(join(outdir, 'index/index.html'))
.then(() => true)
.catch(() => false)
).toBe(false)

expect(
await access(join(outdir, 'index.html'))
.then(() => true)
.catch(() => false)
).toBe(true)
expect(await fileExist(join(outdir, 'index/index.html'))).toBe(false)

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

ssr(context)
Expand Down