Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3423,7 +3423,7 @@ export default async function build(
return staticGenerationSpan
.traceChild('move-exported-app-not-found-')
.traceAsyncFn(async () => {
const orig = path.join(
const underscoreNotFoundHtml = path.join(
distDir,
'server',
'app',
Expand All @@ -3433,9 +3433,10 @@ export default async function build(
.join('pages', '404.html')
.replace(/\\/g, '/')

if (existsSync(orig)) {
// If the _not-found.html exists, use it instead of the pages 404.html
if (existsSync(underscoreNotFoundHtml)) {
await fs.copyFile(
orig,
underscoreNotFoundHtml,
path.join(distDir, 'server', updatedRelativeDest)
)

Expand Down Expand Up @@ -3636,6 +3637,23 @@ export default async function build(
await writeManifest(pagesManifestPath, pagesManifest)
})

const hasStaticAppRouterNotFound = existsSync(
path.join(distDir, 'server', 'app', '_not-found.html')
)
// If the _not-found.html exists, add /_not-found to the prerender manifest
if (hasStaticAppRouterNotFound) {
prerenderManifest.routes['/_not-found'] = {
initialRevalidateSeconds: false,
initialExpireSeconds: undefined,
experimentalPPR: undefined,
renderingMode: undefined,
srcRoute: null,
dataRoute: null,
prefetchDataRoute: null,
allowHeader: ALLOWED_HEADERS,
}
}

// As we may have modified the dynamicRoutes, we need to sort the
// dynamic routes by page.
routesManifest.dynamicRoutes = sortSortableRouteObjects(
Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,10 @@ async function exportAppImpl(
if (!options.buildExport && prerenderManifest) {
await Promise.all(
Object.keys(prerenderManifest.routes).map(async (unnormalizedRoute) => {
// Skip handling /_not-found route, it will copy the 404.html file later
if (unnormalizedRoute === '/_not-found') {
return
}
const { srcRoute } = prerenderManifest!.routes[unnormalizedRoute]
const appPageName = mapAppRouteToPage.get(srcRoute || '')
const pageName = appPageName || srcRoute || unnormalizedRoute
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,19 @@ describe('app-dir static/dynamic handling', () => {
"initialRevalidateSeconds": false,
"srcRoute": "/",
},
"/_not-found": {
"allowHeader": [
"host",
"x-matched-path",
"x-prerender-revalidate",
"x-prerender-revalidate-if-generated",
"x-next-revalidated-tags",
"x-next-revalidate-tag-token",
],
"dataRoute": "/_not-found.rsc",
"initialRevalidateSeconds": false,
"srcRoute": null,
},
"/api/large-data": {
"allowHeader": [
"host",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('async imports in cacheComponents', () => {

expect(prerenderedRoutes).toMatchInlineSnapshot(`
[
"/_not-found",
"/inside-render/client/async-module",
"/inside-render/client/sync-module",
"/inside-render/route-handler/async-module",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
const staticRoutes = prerenderManifest.routes
expect(Object.keys(staticRoutes).sort()).toEqual([
'/',
'_not-found',
'/suspenseful/static',
])
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
const staticRoutes = prerenderManifest.routes
expect(Object.keys(staticRoutes).sort()).toEqual([
'/',
'_not-found',
'/slow/static',
'/suspenseful/static',
])
Expand Down