Skip to content

Commit 8732a49

Browse files
committed
fix: reinstate catch-all redirect for Next redirect handling
1 parent f81b2c3 commit 8732a49

File tree

2 files changed

+11
-52
lines changed

2 files changed

+11
-52
lines changed

cypress/integration/default/dynamic-routes.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ describe('Dynamic Routing', () => {
4444
expect(res.body).to.contain('Under the Dome')
4545
})
4646
})
47-
it('serves custom 404 on a non-prerendered dynamic route with fallback: false', () => {
47+
it('renders custom 404 on a non-prerendered dynamic route with fallback: false', () => {
4848
cy.request({ url: '/getStaticProps/3/', failOnStatusCode: false }).then((res) => {
4949
expect(res.status).to.eq(404)
50-
expect(res.headers).to.not.have.property('x-render-mode')
50+
expect(res.headers).to.have.property('x-render-mode', 'ssr')
5151
expect(res.body).to.contain('Custom 404')
5252
})
5353
})
@@ -88,10 +88,10 @@ describe('Dynamic Routing', () => {
8888
expect(res.body).to.contain('Under the Dome')
8989
})
9090
})
91-
it('serves custom 404 on a non-prerendered dynamic route with revalidate and fallback: false', () => {
91+
it('renders custom 404 on a non-prerendered dynamic route with revalidate and fallback: false', () => {
9292
cy.request({ url: '/getStaticProps/withRevalidate/3/', failOnStatusCode: false }).then((res) => {
9393
expect(res.status).to.eq(404)
94-
expect(res.headers).to.not.have.property('x-render-mode')
94+
expect(res.headers).to.have.property('x-render-mode', 'ssr')
9595
expect(res.body).to.contain('Custom 404')
9696
})
9797
})

packages/runtime/src/helpers/redirects.ts

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -65,51 +65,6 @@ const generateLocaleRedirects = ({
6565
return redirects
6666
}
6767

68-
const generate404Redirects = ({
69-
staticRouteEntries,
70-
basePath,
71-
i18n,
72-
}: {
73-
staticRouteEntries: Array<[string, SsgRoute]>
74-
basePath: string
75-
i18n: NextConfig['i18n']
76-
}): NetlifyConfig['redirects'] => {
77-
const redirects: NetlifyConfig['redirects'] = []
78-
79-
const isIsr404 = staticRouteEntries.some(
80-
([route, { initialRevalidateSeconds }]) => is404Route(route, i18n) && initialRevalidateSeconds !== false,
81-
)
82-
83-
if (isIsr404) {
84-
redirects.push({
85-
from: `${basePath}/*`,
86-
to: ODB_FUNCTION_PATH,
87-
status: 404,
88-
})
89-
} else if (i18n?.locales?.length) {
90-
i18n.locales.forEach((locale) => {
91-
redirects.push({
92-
from: `${basePath}/${locale}/*`,
93-
to: `${basePath}/server/pages/${locale}/404.html`,
94-
status: 404,
95-
})
96-
})
97-
redirects.push({
98-
from: `${basePath}/*`,
99-
to: `${basePath}/server/pages/${i18n.defaultLocale}/404.html`,
100-
status: 404,
101-
})
102-
} else {
103-
redirects.push({
104-
from: `${basePath}/*`,
105-
to: `${basePath}/server/pages/404.html`,
106-
status: 404,
107-
})
108-
}
109-
110-
return redirects
111-
}
112-
11368
export const generateStaticRedirects = ({
11469
netlifyConfig,
11570
nextConfig: { i18n, basePath },
@@ -249,10 +204,9 @@ const generateDynamicRewrites = ({
249204
return
250205
}
251206
if (route.page in prerenderedDynamicRoutes) {
252-
const { fallback } = prerenderedDynamicRoutes[route.page]
253207
if (matchesMiddleware(middleware, route.page)) {
254208
dynamicRoutesThatMatchMiddleware.push(route.page)
255-
} else if (fallback !== false) {
209+
} else {
256210
dynamicRewrites.push(
257211
...redirectsForNextRoute({ buildId, route: route.page, basePath, to: ODB_FUNCTION_PATH, status: 200, i18n }),
258212
)
@@ -342,7 +296,12 @@ export const generateRedirects = async ({
342296
netlifyConfig.redirects.push(...dynamicRewrites)
343297
routesThatMatchMiddleware.push(...dynamicRoutesThatMatchMiddleware)
344298

345-
netlifyConfig.redirects.push(...generate404Redirects({ staticRouteEntries, basePath, i18n }))
299+
// Final fallback
300+
netlifyConfig.redirects.push({
301+
from: `${basePath}/*`,
302+
to: HANDLER_FUNCTION_PATH,
303+
status: 200,
304+
})
346305

347306
const middlewareMatches = new Set(routesThatMatchMiddleware).size
348307
if (middlewareMatches > 0) {

0 commit comments

Comments
 (0)