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') diff --git a/tests/e2e/page-router.test.ts b/tests/e2e/page-router.test.ts index a2471c980a..35e9123a66 100644 --- a/tests/e2e/page-router.test.ts +++ b/tests/e2e/page-router.test.ts @@ -590,6 +590,21 @@ 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() 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)/) + }) }) test.describe('Page Router with basePath and i18n', () => {