From bee9af06d1082e62577751afbb77e227ea5561ad Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Thu, 10 Nov 2022 11:06:50 +0000 Subject: [PATCH 1/3] fix: redirect isr index pages without a sub path --- packages/runtime/src/helpers/redirects.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/runtime/src/helpers/redirects.ts b/packages/runtime/src/helpers/redirects.ts index 3781712e95..9915d9ed69 100644 --- a/packages/runtime/src/helpers/redirects.ts +++ b/packages/runtime/src/helpers/redirects.ts @@ -143,8 +143,8 @@ const generateStaticIsrRewrites = ({ return } // 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 = route.slice(i18n.defaultLocale.length + 1) || '/' staticRoutePaths.add(route) if (matchesMiddleware(middleware, route)) { staticIsrRoutesThatMatchMiddleware.push(route) From c4b1907a32975fe96d173bade76d8b75ed65884c Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Thu, 10 Nov 2022 11:07:19 +0000 Subject: [PATCH 2/3] test: update homepage to test isr i18n --- demos/default/pages/index.js | 19 ++++++++---- test/__snapshots__/index.spec.js.snap | 44 +++++++++++++-------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/demos/default/pages/index.js b/demos/default/pages/index.js index 0a231445f9..198e9a903e 100644 --- a/demos/default/pages/index.js +++ b/demos/default/pages/index.js @@ -17,13 +17,14 @@ const Index = ({ shows, nodeEnv }) => {
-

This is a demo of a NextJS application with Server-Side Rendering (SSR).

+

This is a demo of a NextJS application with Incremental Static Regeneration (ISR).

-

Server-Side Rendering

+

Incremental Static Regeneration

- 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.

NODE_ENV: {nodeEnv} @@ -176,7 +177,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 @@ -190,7 +191,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 diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index a3223f24f3..c7a39003fb 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -567,10 +567,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/*", @@ -752,10 +752,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, @@ -811,6 +811,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", @@ -949,12 +955,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", @@ -1009,6 +1009,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", @@ -1147,12 +1153,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", @@ -1308,10 +1308,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, @@ -1512,10 +1512,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, From 7cef5fefc1841fce80b61a79d734d24a900517de Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Mon, 16 Jan 2023 10:38:15 +0000 Subject: [PATCH 3/3] fix: increase default locale match specificity to avoid false positives --- packages/runtime/src/helpers/redirects.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime/src/helpers/redirects.ts b/packages/runtime/src/helpers/redirects.ts index 9915d9ed69..575621e4a5 100644 --- a/packages/runtime/src/helpers/redirects.ts +++ b/packages/runtime/src/helpers/redirects.ts @@ -143,7 +143,7 @@ const generateStaticIsrRewrites = ({ return } // The default locale is served from the root, not the localised path - if (i18n?.defaultLocale && route.startsWith(`/${i18n.defaultLocale}`)) { + if (i18n?.defaultLocale && (route.startsWith(`/${i18n.defaultLocale}/`) || route === `/${i18n.defaultLocale}`)) { route = route.slice(i18n.defaultLocale.length + 1) || '/' staticRoutePaths.add(route) if (matchesMiddleware(middleware, route)) {