Skip to content

Commit 14ad486

Browse files
authored
fix: correctly rewrite default locale ISR homepage to ODB handler (#1867)
* fix: redirect isr index pages without a sub path * test: update homepage to test isr i18n * fix: increase default locale match specificity to avoid false positives
1 parent 03c21ed commit 14ad486

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

demos/default/pages/index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ const Index = ({ shows, nodeEnv }) => {
1717
<div>
1818
<Header />
1919

20-
<p>This is a demo of a NextJS application with Server-Side Rendering (SSR).</p>
20+
<p>This is a demo of a NextJS application with Incremental Static Regeneration (ISR).</p>
2121

22-
<h2>Server-Side Rendering</h2>
22+
<h2>Incremental Static Regeneration</h2>
2323

2424
<p>
25-
This page is server-side rendered. It fetches a random list of five TV shows from the TVmaze REST API. Refresh
26-
this page to see it change.
25+
This page is rendered by an On-Demand Builder (ODB) function. It fetches a random list of five TV shows from
26+
the TVmaze REST API. After 60 seconds, the ODB cache is invalidated and the page will be re-rendered on the
27+
next request.
2728
</p>
2829
<code>NODE_ENV: {nodeEnv}</code>
2930

@@ -182,7 +183,7 @@ const Index = ({ shows, nodeEnv }) => {
182183
)
183184
}
184185

185-
Index.getInitialProps = async function () {
186+
export async function getStaticProps(context) {
186187
const dev = process.env.CONTEXT !== 'production'
187188

188189
// Set a random page between 1 and 100
@@ -196,7 +197,13 @@ Index.getInitialProps = async function () {
196197
const res = await fetch(server)
197198
const data = await res.json()
198199

199-
return { shows: data.slice(0, 5), nodeEnv: process.env.NODE_ENV || null }
200+
return {
201+
props: {
202+
shows: data.slice(0, 5),
203+
nodeEnv: process.env.NODE_ENV || null,
204+
},
205+
revalidate: 60,
206+
}
200207
}
201208

202209
export default Index

packages/runtime/src/helpers/redirects.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ const generateStaticIsrRewrites = ({
148148
// appDir routes are a different format, so we need to handle them differently
149149
const isAppDir = isAppDirRoute(srcRoute, appPathRoutes)
150150
// The default locale is served from the root, not the localised path
151-
if (i18n?.defaultLocale && route.startsWith(`/${i18n.defaultLocale}/`)) {
152-
route = route.slice(i18n.defaultLocale.length + 1)
151+
if (i18n?.defaultLocale && (route.startsWith(`/${i18n.defaultLocale}/`) || route === `/${i18n.defaultLocale}`)) {
152+
route = route.slice(i18n.defaultLocale.length + 1) || '/'
153153
staticRoutePaths.add(route)
154154
if (matchesMiddleware(middleware, route)) {
155155
staticIsrRoutesThatMatchMiddleware.push(route)

test/__snapshots__/index.spec.js.snap

+22-22
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,10 @@ Array [
637637
"to": "/fr/",
638638
},
639639
Object {
640-
"force": false,
640+
"force": true,
641641
"from": "/",
642642
"status": 200,
643-
"to": "/.netlify/functions/___netlify-handler",
643+
"to": "/.netlify/builders/___netlify-odb-handler",
644644
},
645645
Object {
646646
"from": "/_ipx/*",
@@ -822,10 +822,10 @@ Array [
822822
"to": "/.netlify/functions/___netlify-handler",
823823
},
824824
Object {
825-
"force": false,
825+
"force": true,
826826
"from": "/_next/data/build-id/en/index.json",
827827
"status": 200,
828-
"to": "/.netlify/functions/___netlify-handler",
828+
"to": "/.netlify/builders/___netlify-odb-handler",
829829
},
830830
Object {
831831
"force": false,
@@ -881,6 +881,12 @@ Array [
881881
"status": 200,
882882
"to": "/.netlify/functions/___netlify-handler",
883883
},
884+
Object {
885+
"force": true,
886+
"from": "/_next/data/build-id/es.json",
887+
"status": 200,
888+
"to": "/.netlify/builders/___netlify-odb-handler",
889+
},
884890
Object {
885891
"force": false,
886892
"from": "/_next/data/build-id/es/500.json",
@@ -1019,12 +1025,6 @@ Array [
10191025
"status": 200,
10201026
"to": "/.netlify/functions/___netlify-handler",
10211027
},
1022-
Object {
1023-
"force": false,
1024-
"from": "/_next/data/build-id/es/index.json",
1025-
"status": 200,
1026-
"to": "/.netlify/functions/___netlify-handler",
1027-
},
10281028
Object {
10291029
"force": false,
10301030
"from": "/_next/data/build-id/es/layouts.json",
@@ -1079,6 +1079,12 @@ Array [
10791079
"status": 200,
10801080
"to": "/.netlify/functions/___netlify-handler",
10811081
},
1082+
Object {
1083+
"force": true,
1084+
"from": "/_next/data/build-id/fr.json",
1085+
"status": 200,
1086+
"to": "/.netlify/builders/___netlify-odb-handler",
1087+
},
10821088
Object {
10831089
"force": false,
10841090
"from": "/_next/data/build-id/fr/500.json",
@@ -1217,12 +1223,6 @@ Array [
12171223
"status": 200,
12181224
"to": "/.netlify/functions/___netlify-handler",
12191225
},
1220-
Object {
1221-
"force": false,
1222-
"from": "/_next/data/build-id/fr/index.json",
1223-
"status": 200,
1224-
"to": "/.netlify/functions/___netlify-handler",
1225-
},
12261226
Object {
12271227
"force": false,
12281228
"from": "/_next/data/build-id/fr/layouts.json",
@@ -1450,10 +1450,10 @@ Array [
14501450
"to": "/.netlify/functions/___netlify-handler",
14511451
},
14521452
Object {
1453-
"force": false,
1454-
"from": "/es/",
1453+
"force": true,
1454+
"from": "/es",
14551455
"status": 200,
1456-
"to": "/.netlify/functions/___netlify-handler",
1456+
"to": "/.netlify/builders/___netlify-odb-handler",
14571457
},
14581458
Object {
14591459
"force": false,
@@ -1678,10 +1678,10 @@ Array [
16781678
"to": "/.netlify/functions/___netlify-handler",
16791679
},
16801680
Object {
1681-
"force": false,
1682-
"from": "/fr/",
1681+
"force": true,
1682+
"from": "/fr",
16831683
"status": 200,
1684-
"to": "/.netlify/functions/___netlify-handler",
1684+
"to": "/.netlify/builders/___netlify-odb-handler",
16851685
},
16861686
Object {
16871687
"force": false,

0 commit comments

Comments
 (0)