Skip to content

fix: correctly rewrite default locale ISR homepage to ODB handler #1867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 16, 2023
19 changes: 13 additions & 6 deletions demos/default/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ const Index = ({ shows, nodeEnv }) => {
<div>
<Header />

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

<h2>Server-Side Rendering</h2>
<h2>Incremental Static Regeneration</h2>

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

Expand Down Expand Up @@ -182,7 +183,7 @@ const Index = ({ shows, nodeEnv }) => {
)
}

Index.getInitialProps = async function () {
export async function getStaticProps(context) {
const dev = process.env.CONTEXT !== 'production'

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

return { shows: data.slice(0, 5), nodeEnv: process.env.NODE_ENV || null }
return {
props: {
shows: data.slice(0, 5),
nodeEnv: process.env.NODE_ENV || null,
},
revalidate: 60,
}
}

export default Index
4 changes: 2 additions & 2 deletions packages/runtime/src/helpers/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ const generateStaticIsrRewrites = ({
// appDir routes are a different format, so we need to handle them differently
const isAppDir = isAppDirRoute(srcRoute, appPathRoutes)
// The default locale is served from the root, not the localised path
if (i18n?.defaultLocale && route.startsWith(`/${i18n.defaultLocale}/`)) {
route = route.slice(i18n.defaultLocale.length + 1)
if (i18n?.defaultLocale && (route.startsWith(`/${i18n.defaultLocale}/`) || route === `/${i18n.defaultLocale}`)) {
route = route.slice(i18n.defaultLocale.length + 1) || '/'
staticRoutePaths.add(route)
if (matchesMiddleware(middleware, route)) {
staticIsrRoutesThatMatchMiddleware.push(route)
Expand Down
44 changes: 22 additions & 22 deletions test/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,10 @@ Array [
"to": "/fr/",
},
Object {
"force": false,
"force": true,
"from": "/",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"from": "/_ipx/*",
Expand Down Expand Up @@ -822,10 +822,10 @@ Array [
"to": "/.netlify/functions/___netlify-handler",
},
Object {
"force": false,
"force": true,
"from": "/_next/data/build-id/en/index.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": false,
Expand Down Expand Up @@ -881,6 +881,12 @@ Array [
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
},
Object {
"force": true,
"from": "/_next/data/build-id/es.json",
"status": 200,
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": false,
"from": "/_next/data/build-id/es/500.json",
Expand Down Expand Up @@ -1019,12 +1025,6 @@ Array [
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
},
Object {
"force": false,
"from": "/_next/data/build-id/es/index.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
},
Object {
"force": false,
"from": "/_next/data/build-id/es/layouts.json",
Expand Down Expand Up @@ -1079,6 +1079,12 @@ Array [
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
},
Object {
"force": true,
"from": "/_next/data/build-id/fr.json",
"status": 200,
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": false,
"from": "/_next/data/build-id/fr/500.json",
Expand Down Expand Up @@ -1217,12 +1223,6 @@ Array [
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
},
Object {
"force": false,
"from": "/_next/data/build-id/fr/index.json",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
},
Object {
"force": false,
"from": "/_next/data/build-id/fr/layouts.json",
Expand Down Expand Up @@ -1450,10 +1450,10 @@ Array [
"to": "/.netlify/functions/___netlify-handler",
},
Object {
"force": false,
"from": "/es/",
"force": true,
"from": "/es",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": false,
Expand Down Expand Up @@ -1678,10 +1678,10 @@ Array [
"to": "/.netlify/functions/___netlify-handler",
},
Object {
"force": false,
"from": "/fr/",
"force": true,
"from": "/fr",
"status": 200,
"to": "/.netlify/functions/___netlify-handler",
"to": "/.netlify/builders/___netlify-odb-handler",
},
Object {
"force": false,
Expand Down