Skip to content

Commit 18c2333

Browse files
committed
feat: add support for isr 404 pages
1 parent 52ee02c commit 18c2333

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

packages/runtime/src/helpers/redirects.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { RoutesManifest } from './types'
1414
import {
1515
getApiRewrites,
1616
getPreviewRewrites,
17+
is404Route,
1718
isApiRoute,
1819
redirectsForNextRoute,
1920
redirectsForNextRouteWithData,
@@ -65,12 +66,30 @@ const generateLocaleRedirects = ({
6566
}
6667

6768
const generate404Redirects = ({
68-
i18n,
69+
staticRouteEntries,
6970
basePath,
70-
}: Pick<NextConfig, 'i18n' | 'basePath'>): NetlifyConfig['redirects'] => {
71+
i18n,
72+
buildId,
73+
}: {
74+
staticRouteEntries: Array<[string, SsgRoute]>
75+
basePath: string
76+
i18n: NextConfig['i18n']
77+
buildId: string
78+
}): NetlifyConfig['redirects'] => {
7179
const redirects: NetlifyConfig['redirects'] = []
7280

73-
if (i18n) {
81+
const isIsr404 = staticRouteEntries.some(
82+
([route, { initialRevalidateSeconds }]) => is404Route(route, i18n) && initialRevalidateSeconds !== false,
83+
)
84+
85+
if (isIsr404) {
86+
redirects.push({
87+
from: `${basePath}/*`,
88+
to: ODB_FUNCTION_PATH,
89+
status: 404,
90+
},
91+
)
92+
} else if (i18n?.locales?.length) {
7493
i18n.locales.forEach((locale) => {
7594
redirects.push({
7695
from: `${basePath}/${locale}/*`,
@@ -161,7 +180,7 @@ const generateStaticIsrRewrites = ({
161180
const staticRoutePaths = new Set<string>()
162181
const staticIsrRewrites: NetlifyConfig['redirects'] = []
163182
staticRouteEntries.forEach(([route, { initialRevalidateSeconds }]) => {
164-
if (isApiRoute(route)) {
183+
if (isApiRoute(route) || is404Route(route, i18n)) {
165184
return
166185
}
167186
staticRoutePaths.add(route)
@@ -229,7 +248,7 @@ const generateDynamicRewrites = ({
229248
const dynamicRewrites: NetlifyConfig['redirects'] = []
230249
const dynamicRoutesThatMatchMiddleware: Array<string> = []
231250
dynamicRoutes.forEach((route) => {
232-
if (isApiRoute(route.page)) {
251+
if (isApiRoute(route.page) || is404Route(route.page, i18n)) {
233252
return
234253
}
235254
if (route.page in prerenderedDynamicRoutes) {
@@ -306,7 +325,7 @@ export const generateRedirects = async ({
306325

307326
// Add rewrites for all static SSR routes. This is Next 12+
308327
staticRoutes?.forEach((route) => {
309-
if (staticRoutePaths.has(route.page) || isApiRoute(route.page)) {
328+
if (staticRoutePaths.has(route.page) || isApiRoute(route.page) || is404Route(route.page)) {
310329
// Prerendered static routes are either handled by the CDN or are ISR
311330
return
312331
}
@@ -326,7 +345,7 @@ export const generateRedirects = async ({
326345
netlifyConfig.redirects.push(...dynamicRewrites)
327346
routesThatMatchMiddleware.push(...dynamicRoutesThatMatchMiddleware)
328347

329-
netlifyConfig.redirects.push(...generate404Redirects({ i18n, basePath }))
348+
netlifyConfig.redirects.push(...generate404Redirects({ staticRouteEntries, basePath, i18n, buildId }))
330349

331350
const middlewareMatches = new Set(routesThatMatchMiddleware).size
332351
if (middlewareMatches > 0) {

packages/runtime/src/helpers/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ const netlifyRoutesForNextRoute = (route: string, buildId: string, i18n?: I18n):
7373

7474
export const isApiRoute = (route: string) => route.startsWith('/api/') || route === '/api'
7575

76+
export const is404Route = (route: string, i18n?: I18n) =>
77+
i18n ? i18n.locales.some((locale) => route === `/${locale}/404`) : route === '/404'
78+
7679
export const redirectsForNextRoute = ({
7780
route,
7881
buildId,

0 commit comments

Comments
 (0)