From c75b3ead3476cd7f69bb2ead1d888795faf8d31b Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Wed, 2 Apr 2025 19:10:55 +0200 Subject: [PATCH 1/3] test: api route calling res.revalidate on path returning notFound: true is not cacheable --- tests/e2e/page-router.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/e2e/page-router.test.ts b/tests/e2e/page-router.test.ts index a2471c980a..45b04a4050 100644 --- a/tests/e2e/page-router.test.ts +++ b/tests/e2e/page-router.test.ts @@ -590,6 +590,15 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => { 'getStaticProps duration should not be longer than 10 seconds', ).toBeLessThan(10_000) }) + + test('API route calling res.revalidate() is not cacheable', async ({ page, pageRouter }) => { + const response = await page.goto( + new URL('/api/revalidate?path=/static/not-found', pageRouter.url).href, + ) + + expect(response?.status()).toEqual(200) + expect(response?.headers()['netlify-cdn-cache-control']).not.toMatch(/(s-maxage|max-age)/) + }) }) test.describe('Page Router with basePath and i18n', () => { From 5eb3f33bcc1313e2a5148d827b582867f88850af Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Wed, 2 Apr 2025 11:45:20 +0200 Subject: [PATCH 2/3] fix: don't set permanent caching header when res.revalidate() was used --- src/run/headers.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/run/headers.ts b/src/run/headers.ts index 1931f61330..35a1a4b37a 100644 --- a/src/run/headers.ts +++ b/src/run/headers.ts @@ -266,7 +266,8 @@ export const setCacheControlHeaders = ( ['GET', 'HEAD'].includes(request.method) && !headers.has('cdn-cache-control') && !headers.has('netlify-cdn-cache-control') && - requestContext.usedFsReadForNonFallback + requestContext.usedFsReadForNonFallback && + !requestContext.didPagesRouterOnDemandRevalidate ) { // handle CDN Cache Control on static files headers.set('cache-control', 'public, max-age=0, must-revalidate') From 09eedcdd631957a45596a104aecbd19519e87232 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Wed, 2 Apr 2025 19:36:49 +0200 Subject: [PATCH 3/3] test: adjust added test name, add some context, handle case of header not being set --- tests/e2e/page-router.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/e2e/page-router.test.ts b/tests/e2e/page-router.test.ts index 45b04a4050..35e9123a66 100644 --- a/tests/e2e/page-router.test.ts +++ b/tests/e2e/page-router.test.ts @@ -591,13 +591,19 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => { ).toBeLessThan(10_000) }) - test('API route calling res.revalidate() is not cacheable', async ({ page, pageRouter }) => { + test('API route calling res.revalidate() on page returning notFound: true is not cacheable', async ({ + page, + pageRouter, + }) => { + // note: known conditions for problematic case is + // 1. API route needs to call res.revalidate() + // 2. revalidated page's getStaticProps must return notFound: true const response = await page.goto( new URL('/api/revalidate?path=/static/not-found', pageRouter.url).href, ) expect(response?.status()).toEqual(200) - expect(response?.headers()['netlify-cdn-cache-control']).not.toMatch(/(s-maxage|max-age)/) + expect(response?.headers()['netlify-cdn-cache-control'] ?? '').not.toMatch(/(s-maxage|max-age)/) }) })