From 75b4dc9d156cb0de07f3d796578ff2b7ebf7ea9c Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Wed, 26 Jul 2023 12:32:35 +0100 Subject: [PATCH 1/3] fix: ensure isr 404 pages are only cached for 60s --- packages/runtime/src/templates/getHandler.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/runtime/src/templates/getHandler.ts b/packages/runtime/src/templates/getHandler.ts index b25378e9f2..657a934ebb 100644 --- a/packages/runtime/src/templates/getHandler.ts +++ b/packages/runtime/src/templates/getHandler.ts @@ -163,6 +163,11 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod // Sending SWR headers causes undefined behaviour with the Netlify CDN const cacheHeader = multiValueHeaders['cache-control']?.[0] + // special case for ISR 404s + if (result.statusCode === 404 && requestMode === 'odb') { + result.ttl = 60 + } + if (cacheHeader?.includes('stale-while-revalidate')) { if (requestMode === 'odb') { const ttl = getMaxAge(cacheHeader) @@ -176,12 +181,14 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod // Only cache for 60s if default TTL provided result.ttl = 60 } - if (result.ttl > 0) { - requestMode = `odb ttl=${result.ttl}` - } } multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate'] } + + if (result.ttl > 0) { + requestMode = `odb ttl=${result.ttl}` + } + multiValueHeaders['x-nf-render-mode'] = [requestMode] console.log(`[${event.httpMethod}] ${event.path} (${requestMode?.toUpperCase()})`) From ec9977673e16634e2392a76cdcc5a8bff2cc4086 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Wed, 26 Jul 2023 16:25:39 +0100 Subject: [PATCH 2/3] chore: refactor isr 404 code under main SWR logic --- packages/runtime/src/templates/getHandler.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/runtime/src/templates/getHandler.ts b/packages/runtime/src/templates/getHandler.ts index 657a934ebb..e6187970ec 100644 --- a/packages/runtime/src/templates/getHandler.ts +++ b/packages/runtime/src/templates/getHandler.ts @@ -163,11 +163,6 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod // Sending SWR headers causes undefined behaviour with the Netlify CDN const cacheHeader = multiValueHeaders['cache-control']?.[0] - // special case for ISR 404s - if (result.statusCode === 404 && requestMode === 'odb') { - result.ttl = 60 - } - if (cacheHeader?.includes('stale-while-revalidate')) { if (requestMode === 'odb') { const ttl = getMaxAge(cacheHeader) @@ -185,6 +180,11 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate'] } + // ISR 404s are not served with SWR headers so we need to set the TTL here + if (requestMode === 'odb' && result.statusCode === 404) { + result.ttl = 60 + } + if (result.ttl > 0) { requestMode = `odb ttl=${result.ttl}` } From 2b71788f017176c96f89dac71e3642c15752f072 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Thu, 27 Jul 2023 12:48:53 +0100 Subject: [PATCH 3/3] chore: remove unnecessary 404 code from list of ephemeral codes --- packages/runtime/src/templates/getHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime/src/templates/getHandler.ts b/packages/runtime/src/templates/getHandler.ts index e6187970ec..1c6fa23522 100644 --- a/packages/runtime/src/templates/getHandler.ts +++ b/packages/runtime/src/templates/getHandler.ts @@ -171,7 +171,7 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod // ODBs currently have a minimum TTL of 60 seconds result.ttl = Math.max(ttl, 60) } - const ephemeralCodes = [301, 302, 307, 308, 404] + const ephemeralCodes = [301, 302, 307, 308] if (ttl === ONE_YEAR_IN_SECONDS && ephemeralCodes.includes(result.statusCode)) { // Only cache for 60s if default TTL provided result.ttl = 60