Skip to content

Commit

Permalink
Ensure /404 is not exported during build un-necessarily (#22825)
Browse files Browse the repository at this point in the history
This ensures we don't export `/404` during the automatic static optimization during build when the `/404` isn't static and won't be used/copied to the final output. 

x-ref: #22815
  • Loading branch information
ijjk authored Mar 8, 2021
1 parent 3f9c469 commit cdf8f9a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,11 @@ export default async function exportApp(
})
)

if (!exportPathMap['/404'] && !exportPathMap['/404.html']) {
if (
!options.buildExport &&
!exportPathMap['/404'] &&
!exportPathMap['/404.html']
) {
exportPathMap['/404'] = exportPathMap['/404.html'] = {
page: '/_error',
}
Expand Down
9 changes: 6 additions & 3 deletions test/integration/static-404/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,18 @@ describe('Static 404 page', () => {
`
import Error from 'next/error'
export default class MyError extends Error {
static getInitialProps() {
static getInitialProps({ statusCode, req }) {
if (req.url === '/404' || req.url === '/404.html') {
throw new Error('exported 404 unexpectedly!!!')
}
return {
statusCode: 404
statusCode,
}
}
}
`
)
await nextBuild(appDir)
await nextBuild(appDir, undefined, { stderr: true, stdout: true })
await fs.remove(errorPage)
})

Expand Down

0 comments on commit cdf8f9a

Please sign in to comment.