Skip to content

Commit a0cc3f7

Browse files
committed
fix: rewrite fallback false routes to 404 page
1 parent d62bc56 commit a0cc3f7

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

packages/runtime/src/helpers/redirects.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
isApiRoute,
2020
redirectsForNextRoute,
2121
redirectsForNextRouteWithData,
22+
redirectsForNext404Route,
2223
routeToDataRoute,
2324
} from './utils'
2425

@@ -207,9 +208,11 @@ const generateDynamicRewrites = ({
207208
if (route.page in prerenderedDynamicRoutes) {
208209
if (matchesMiddleware(middleware, route.page)) {
209210
dynamicRoutesThatMatchMiddleware.push(route.page)
211+
} else if (prerenderedDynamicRoutes[route.page].fallback === false) {
212+
dynamicRewrites.push(...redirectsForNext404Route({ route: route.page, buildId, basePath, i18n }))
210213
} else {
211214
dynamicRewrites.push(
212-
...redirectsForNextRoute({ buildId, route: route.page, basePath, to: ODB_FUNCTION_PATH, status: 200, i18n }),
215+
...redirectsForNextRoute({ route: route.page, buildId, basePath, to: ODB_FUNCTION_PATH, i18n }),
213216
)
214217
}
215218
} else {

packages/runtime/src/helpers/utils.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,18 @@ export const netlifyRoutesForNextRouteWithData = ({ route, dataRoute }: { route:
7272
export const routeToDataRoute = (route: string, buildId: string, locale?: string) =>
7373
`/_next/data/${buildId}${locale ? `/${locale}` : ''}${route === '/' ? '/index' : route}.json`
7474

75-
const netlifyRoutesForNextRoute = (route: string, buildId: string, i18n?: I18n): Array<string> => {
75+
const netlifyRoutesForNextRoute = (
76+
route: string,
77+
buildId: string,
78+
i18n?: I18n,
79+
): Array<{ redirect: string; locale: string | false }> => {
7680
if (!i18n?.locales?.length) {
77-
return netlifyRoutesForNextRouteWithData({ route, dataRoute: routeToDataRoute(route, buildId) })
81+
return netlifyRoutesForNextRouteWithData({ route, dataRoute: routeToDataRoute(route, buildId) }).map(
82+
(redirect) => ({
83+
redirect,
84+
locale: false,
85+
}),
86+
)
7887
}
7988
const { locales, defaultLocale } = i18n
8089
const routes = []
@@ -87,7 +96,10 @@ const netlifyRoutesForNextRoute = (route: string, buildId: string, i18n?: I18n):
8796
...netlifyRoutesForNextRouteWithData({
8897
route: locale === defaultLocale ? route : `/${locale}${route}`,
8998
dataRoute,
90-
}),
99+
}).map((redirect) => ({
100+
redirect,
101+
locale,
102+
})),
91103
)
92104
})
93105
return routes
@@ -115,13 +127,33 @@ export const redirectsForNextRoute = ({
115127
status?: number
116128
force?: boolean
117129
}): NetlifyConfig['redirects'] =>
118-
netlifyRoutesForNextRoute(route, buildId, i18n).map((redirect) => ({
130+
netlifyRoutesForNextRoute(route, buildId, i18n).map(({ redirect }) => ({
119131
from: `${basePath}${redirect}`,
120132
to,
121133
status,
122134
force,
123135
}))
124136

137+
export const redirectsForNext404Route = ({
138+
route,
139+
buildId,
140+
basePath,
141+
i18n,
142+
force = false,
143+
}: {
144+
route: string
145+
buildId: string
146+
basePath: string
147+
i18n: I18n
148+
force?: boolean
149+
}): NetlifyConfig['redirects'] =>
150+
netlifyRoutesForNextRoute(route, buildId, i18n).map(({ redirect, locale }) => ({
151+
from: `${basePath}${redirect}`,
152+
to: locale ? `${basePath}/server/pages/${locale}/404.html` : `${basePath}/server/pages/404.html`,
153+
status: 404,
154+
force,
155+
}))
156+
125157
export const redirectsForNextRouteWithData = ({
126158
route,
127159
dataRoute,

0 commit comments

Comments
 (0)