From 060740575aed714ffd4f28d92f66edb1a528d927 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 30 Sep 2022 19:26:20 +0100 Subject: [PATCH 1/6] fix: cache 404s for max 60 seconds --- packages/runtime/src/templates/getHandler.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/runtime/src/templates/getHandler.ts b/packages/runtime/src/templates/getHandler.ts index c6fbff2bb5..6ecbd1c2b2 100644 --- a/packages/runtime/src/templates/getHandler.ts +++ b/packages/runtime/src/templates/getHandler.ts @@ -128,8 +128,15 @@ const makeHandler = (conf: NextConfig, app, pageRoot, staticManifest: Array<[str const ttl = getMaxAge(cacheHeader) // Long-expiry TTL is basically no TTL, so we'll skip it if (ttl > 0 && ttl < ONE_YEAR_IN_SECONDS) { - result.ttl = ttl - requestMode = `odb ttl=${ttl}` + // ODBs currently have a minimum TTL of 60 seconds + result.ttl = Math.max(ttl, 60) + } + // Only cache 404s ephemerally + if (ttl === ONE_YEAR_IN_SECONDS && result.statusCode === 404) { + result.ttl = 60 + } + if (result.ttl > 0) { + requestMode = `odb ttl=${result.ttl}` } } multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate'] From 6ce930b1f90d0e3bad3b530a70659183fcbb8a33 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Mon, 3 Oct 2022 12:38:19 +0100 Subject: [PATCH 2/6] feat: add new demo page to test ISR returning notFound --- .../getStaticProps/with-revalidate-404.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 demos/default/pages/getStaticProps/with-revalidate-404.js diff --git a/demos/default/pages/getStaticProps/with-revalidate-404.js b/demos/default/pages/getStaticProps/with-revalidate-404.js new file mode 100644 index 0000000000..2aa192f373 --- /dev/null +++ b/demos/default/pages/getStaticProps/with-revalidate-404.js @@ -0,0 +1,35 @@ +import Link from 'next/link' + +const Show = ({ show }) => ( +
+

+ This page uses getStaticProps() to pre-fetch a TV show, but will return a 404 if the current time ends in 0-4. +

+ +
+ +

Show #{show.id}

+

{show.name}

+ +
+ + + Go back home + +
+) + +export async function getStaticProps(context) { + const res = await fetch(`https://api.tvmaze.com/shows/71`) + const data = await res.json() + + return { + props: { + show: data, + }, + notFound: new Date().getMinutes() % 10 < 5, + revalidate: 60, + } +} + +export default Show From 34e72c27c9f12c9dd5225126618b836934575f97 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Mon, 3 Oct 2022 19:12:22 +0100 Subject: [PATCH 3/6] test: update test snapshots with new page --- test/__snapshots__/index.js.snap | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/__snapshots__/index.js.snap b/test/__snapshots__/index.js.snap index bb77f6a9a4..5256ac3769 100644 --- a/test/__snapshots__/index.js.snap +++ b/test/__snapshots__/index.js.snap @@ -20,6 +20,7 @@ exports.resolvePages = () => { require.resolve('../../../.next/server/pages/getStaticProps/[id].js') require.resolve('../../../.next/server/pages/getStaticProps/env.js') require.resolve('../../../.next/server/pages/getStaticProps/static.js') + require.resolve('../../../.next/server/pages/getStaticProps/with-revalidate-404.js') require.resolve('../../../.next/server/pages/getStaticProps/with-revalidate.js') require.resolve('../../../.next/server/pages/getStaticProps/withFallback/[...slug].js') require.resolve('../../../.next/server/pages/getStaticProps/withFallback/[id].js') @@ -55,6 +56,7 @@ exports.resolvePages = () => { require.resolve('../../../.next/server/pages/getStaticProps/[id].js') require.resolve('../../../.next/server/pages/getStaticProps/env.js') require.resolve('../../../.next/server/pages/getStaticProps/static.js') + require.resolve('../../../.next/server/pages/getStaticProps/with-revalidate-404.js') require.resolve('../../../.next/server/pages/getStaticProps/with-revalidate.js') require.resolve('../../../.next/server/pages/getStaticProps/withFallback/[...slug].js') require.resolve('../../../.next/server/pages/getStaticProps/withFallback/[id].js') @@ -90,6 +92,7 @@ exports.resolvePages = () => { require.resolve('../../../web/.next/server/pages/getStaticProps/[id].js') require.resolve('../../../web/.next/server/pages/getStaticProps/env.js') require.resolve('../../../web/.next/server/pages/getStaticProps/static.js') + require.resolve('../../../web/.next/server/pages/getStaticProps/with-revalidate-404.js') require.resolve('../../../web/.next/server/pages/getStaticProps/with-revalidate.js') require.resolve('../../../web/.next/server/pages/getStaticProps/withFallback/[...slug].js') require.resolve('../../../web/.next/server/pages/getStaticProps/withFallback/[id].js') @@ -125,6 +128,7 @@ exports.resolvePages = () => { require.resolve('../../../web/.next/server/pages/getStaticProps/[id].js') require.resolve('../../../web/.next/server/pages/getStaticProps/env.js') require.resolve('../../../web/.next/server/pages/getStaticProps/static.js') + require.resolve('../../../web/.next/server/pages/getStaticProps/with-revalidate-404.js') require.resolve('../../../web/.next/server/pages/getStaticProps/with-revalidate.js') require.resolve('../../../web/.next/server/pages/getStaticProps/withFallback/[...slug].js') require.resolve('../../../web/.next/server/pages/getStaticProps/withFallback/[id].js') @@ -501,6 +505,12 @@ Array [ "status": 200, "to": "/.netlify/functions/___netlify-handler", }, + Object { + "force": true, + "from": "/_next/data/build-id/en/getStaticProps/with-revalidate-404.json", + "status": 200, + "to": "/.netlify/builders/___netlify-odb-handler", + }, Object { "force": true, "from": "/_next/data/build-id/en/getStaticProps/with-revalidate.json", @@ -723,6 +733,12 @@ Array [ "status": 200, "to": "/.netlify/functions/___netlify-handler", }, + Object { + "force": true, + "from": "/_next/data/build-id/es/getStaticProps/with-revalidate-404.json", + "status": 200, + "to": "/.netlify/builders/___netlify-odb-handler", + }, Object { "force": true, "from": "/_next/data/build-id/es/getStaticProps/with-revalidate.json", @@ -909,6 +925,12 @@ Array [ "status": 200, "to": "/.netlify/functions/___netlify-handler", }, + Object { + "force": true, + "from": "/_next/data/build-id/fr/getStaticProps/with-revalidate-404.json", + "status": 200, + "to": "/.netlify/builders/___netlify-odb-handler", + }, Object { "force": true, "from": "/_next/data/build-id/fr/getStaticProps/with-revalidate.json", @@ -1202,6 +1224,12 @@ Array [ "status": 200, "to": "/.netlify/builders/___netlify-odb-handler", }, + Object { + "force": true, + "from": "/es/getStaticProps/with-revalidate-404", + "status": 200, + "to": "/.netlify/builders/___netlify-odb-handler", + }, Object { "force": false, "from": "/es/getStaticProps/withFallback/:id", @@ -1394,6 +1422,12 @@ Array [ "status": 200, "to": "/.netlify/builders/___netlify-odb-handler", }, + Object { + "force": true, + "from": "/fr/getStaticProps/with-revalidate-404", + "status": 200, + "to": "/.netlify/builders/___netlify-odb-handler", + }, Object { "force": false, "from": "/fr/getStaticProps/withFallback/:id", @@ -1538,6 +1572,12 @@ Array [ "status": 200, "to": "/.netlify/builders/___netlify-odb-handler", }, + Object { + "force": true, + "from": "/getStaticProps/with-revalidate-404", + "status": 200, + "to": "/.netlify/builders/___netlify-odb-handler", + }, Object { "force": false, "from": "/getStaticProps/withFallback/:id", From 62c42a2177b2f36c93f8f9f38eca5d3687fe8f7c Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Mon, 3 Oct 2022 19:39:14 +0100 Subject: [PATCH 4/6] chore: debug test errors --- demos/default/pages/getStaticProps/static.js | 1 + demos/default/pages/getStaticProps/with-revalidate-404.js | 1 + demos/default/pages/getStaticProps/with-revalidate.js | 1 + 3 files changed, 3 insertions(+) diff --git a/demos/default/pages/getStaticProps/static.js b/demos/default/pages/getStaticProps/static.js index 13d6428e22..f0cffdc4c4 100644 --- a/demos/default/pages/getStaticProps/static.js +++ b/demos/default/pages/getStaticProps/static.js @@ -19,6 +19,7 @@ const Show = ({ show }) => ( export async function getStaticProps(context) { const res = await fetch(`https://api.tvmaze.com/shows/71`) + console.log(res) const data = await res.json() return { diff --git a/demos/default/pages/getStaticProps/with-revalidate-404.js b/demos/default/pages/getStaticProps/with-revalidate-404.js index 2aa192f373..d7ee8da42d 100644 --- a/demos/default/pages/getStaticProps/with-revalidate-404.js +++ b/demos/default/pages/getStaticProps/with-revalidate-404.js @@ -21,6 +21,7 @@ const Show = ({ show }) => ( export async function getStaticProps(context) { const res = await fetch(`https://api.tvmaze.com/shows/71`) + console.log(res) const data = await res.json() return { diff --git a/demos/default/pages/getStaticProps/with-revalidate.js b/demos/default/pages/getStaticProps/with-revalidate.js index dda98ce051..83d2607c43 100644 --- a/demos/default/pages/getStaticProps/with-revalidate.js +++ b/demos/default/pages/getStaticProps/with-revalidate.js @@ -19,6 +19,7 @@ const Show = ({ show }) => ( export async function getStaticProps(context) { const res = await fetch(`https://api.tvmaze.com/shows/71`) + console.log(res) const data = await res.json() return { From 51c4cd3a471021af757bd44a33124d0efdb05801 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Mon, 3 Oct 2022 19:44:35 +0100 Subject: [PATCH 5/6] chore: update test debugging --- demos/default/pages/getStaticProps/static.js | 2 +- demos/default/pages/getStaticProps/with-revalidate-404.js | 2 +- demos/default/pages/getStaticProps/with-revalidate.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/demos/default/pages/getStaticProps/static.js b/demos/default/pages/getStaticProps/static.js index f0cffdc4c4..aaec09d508 100644 --- a/demos/default/pages/getStaticProps/static.js +++ b/demos/default/pages/getStaticProps/static.js @@ -19,8 +19,8 @@ const Show = ({ show }) => ( export async function getStaticProps(context) { const res = await fetch(`https://api.tvmaze.com/shows/71`) - console.log(res) const data = await res.json() + console.log(data) return { props: { diff --git a/demos/default/pages/getStaticProps/with-revalidate-404.js b/demos/default/pages/getStaticProps/with-revalidate-404.js index d7ee8da42d..fff10f57bd 100644 --- a/demos/default/pages/getStaticProps/with-revalidate-404.js +++ b/demos/default/pages/getStaticProps/with-revalidate-404.js @@ -21,8 +21,8 @@ const Show = ({ show }) => ( export async function getStaticProps(context) { const res = await fetch(`https://api.tvmaze.com/shows/71`) - console.log(res) const data = await res.json() + console.log(data) return { props: { diff --git a/demos/default/pages/getStaticProps/with-revalidate.js b/demos/default/pages/getStaticProps/with-revalidate.js index 83d2607c43..49feab3f29 100644 --- a/demos/default/pages/getStaticProps/with-revalidate.js +++ b/demos/default/pages/getStaticProps/with-revalidate.js @@ -19,8 +19,8 @@ const Show = ({ show }) => ( export async function getStaticProps(context) { const res = await fetch(`https://api.tvmaze.com/shows/71`) - console.log(res) const data = await res.json() + console.log(data) return { props: { From b6d5e6b9b16e15a15a56ddaec1589a8267fc83ac Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Mon, 3 Oct 2022 19:51:55 +0100 Subject: [PATCH 6/6] fix: remove api call from new revalidate page --- demos/default/pages/getStaticProps/static.js | 1 - .../getStaticProps/with-revalidate-404.js | 21 +++---------------- .../pages/getStaticProps/with-revalidate.js | 1 - 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/demos/default/pages/getStaticProps/static.js b/demos/default/pages/getStaticProps/static.js index aaec09d508..13d6428e22 100644 --- a/demos/default/pages/getStaticProps/static.js +++ b/demos/default/pages/getStaticProps/static.js @@ -20,7 +20,6 @@ const Show = ({ show }) => ( export async function getStaticProps(context) { const res = await fetch(`https://api.tvmaze.com/shows/71`) const data = await res.json() - console.log(data) return { props: { diff --git a/demos/default/pages/getStaticProps/with-revalidate-404.js b/demos/default/pages/getStaticProps/with-revalidate-404.js index fff10f57bd..c5658021b8 100644 --- a/demos/default/pages/getStaticProps/with-revalidate-404.js +++ b/demos/default/pages/getStaticProps/with-revalidate-404.js @@ -1,17 +1,8 @@ import Link from 'next/link' -const Show = ({ show }) => ( +const Show = () => (
-

- This page uses getStaticProps() to pre-fetch a TV show, but will return a 404 if the current time ends in 0-4. -

- -
- -

Show #{show.id}

-

{show.name}

- -
+

This page is ISR, but will return a 404 if the current time ends in 0-4.

Go back home @@ -20,14 +11,8 @@ const Show = ({ show }) => ( ) export async function getStaticProps(context) { - const res = await fetch(`https://api.tvmaze.com/shows/71`) - const data = await res.json() - console.log(data) - return { - props: { - show: data, - }, + props: {}, notFound: new Date().getMinutes() % 10 < 5, revalidate: 60, } diff --git a/demos/default/pages/getStaticProps/with-revalidate.js b/demos/default/pages/getStaticProps/with-revalidate.js index 49feab3f29..dda98ce051 100644 --- a/demos/default/pages/getStaticProps/with-revalidate.js +++ b/demos/default/pages/getStaticProps/with-revalidate.js @@ -20,7 +20,6 @@ const Show = ({ show }) => ( export async function getStaticProps(context) { const res = await fetch(`https://api.tvmaze.com/shows/71`) const data = await res.json() - console.log(data) return { props: {