@@ -14,6 +14,7 @@ import { RoutesManifest } from './types'
14
14
import {
15
15
getApiRewrites ,
16
16
getPreviewRewrites ,
17
+ is404Route ,
17
18
isApiRoute ,
18
19
redirectsForNextRoute ,
19
20
redirectsForNextRouteWithData ,
@@ -65,12 +66,30 @@ const generateLocaleRedirects = ({
65
66
}
66
67
67
68
const generate404Redirects = ( {
68
- i18n ,
69
+ staticRouteEntries ,
69
70
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' ] => {
71
79
const redirects : NetlifyConfig [ 'redirects' ] = [ ]
72
80
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 ) {
74
93
i18n . locales . forEach ( ( locale ) => {
75
94
redirects . push ( {
76
95
from : `${ basePath } /${ locale } /*` ,
@@ -161,7 +180,7 @@ const generateStaticIsrRewrites = ({
161
180
const staticRoutePaths = new Set < string > ( )
162
181
const staticIsrRewrites : NetlifyConfig [ 'redirects' ] = [ ]
163
182
staticRouteEntries . forEach ( ( [ route , { initialRevalidateSeconds } ] ) => {
164
- if ( isApiRoute ( route ) ) {
183
+ if ( isApiRoute ( route ) || is404Route ( route , i18n ) ) {
165
184
return
166
185
}
167
186
staticRoutePaths . add ( route )
@@ -229,7 +248,7 @@ const generateDynamicRewrites = ({
229
248
const dynamicRewrites : NetlifyConfig [ 'redirects' ] = [ ]
230
249
const dynamicRoutesThatMatchMiddleware : Array < string > = [ ]
231
250
dynamicRoutes . forEach ( ( route ) => {
232
- if ( isApiRoute ( route . page ) ) {
251
+ if ( isApiRoute ( route . page ) || is404Route ( route . page , i18n ) ) {
233
252
return
234
253
}
235
254
if ( route . page in prerenderedDynamicRoutes ) {
@@ -306,7 +325,7 @@ export const generateRedirects = async ({
306
325
307
326
// Add rewrites for all static SSR routes. This is Next 12+
308
327
staticRoutes ?. forEach ( ( route ) => {
309
- if ( staticRoutePaths . has ( route . page ) || isApiRoute ( route . page ) ) {
328
+ if ( staticRoutePaths . has ( route . page ) || isApiRoute ( route . page ) || is404Route ( route . page ) ) {
310
329
// Prerendered static routes are either handled by the CDN or are ISR
311
330
return
312
331
}
@@ -326,7 +345,7 @@ export const generateRedirects = async ({
326
345
netlifyConfig . redirects . push ( ...dynamicRewrites )
327
346
routesThatMatchMiddleware . push ( ...dynamicRoutesThatMatchMiddleware )
328
347
329
- netlifyConfig . redirects . push ( ...generate404Redirects ( { i18n , basePath } ) )
348
+ netlifyConfig . redirects . push ( ...generate404Redirects ( { staticRouteEntries , basePath, i18n , buildId } ) )
330
349
331
350
const middlewareMatches = new Set ( routesThatMatchMiddleware ) . size
332
351
if ( middlewareMatches > 0 ) {
0 commit comments