Skip to content

Commit 0e7e3a2

Browse files
piehmrstork
andauthored
fix: don't set permanent caching header when res.revalidate() was used (#2810)
* test: api route calling res.revalidate on path returning notFound: true is not cacheable * fix: don't set permanent caching header when res.revalidate() was used * test: adjust added test name, add some context, handle case of header not being set --------- Co-authored-by: Mateusz Bocian <[email protected]>
1 parent f1b8509 commit 0e7e3a2

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/run/headers.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ export const setCacheControlHeaders = (
266266
['GET', 'HEAD'].includes(request.method) &&
267267
!headers.has('cdn-cache-control') &&
268268
!headers.has('netlify-cdn-cache-control') &&
269-
requestContext.usedFsReadForNonFallback
269+
requestContext.usedFsReadForNonFallback &&
270+
!requestContext.didPagesRouterOnDemandRevalidate
270271
) {
271272
// handle CDN Cache Control on static files
272273
headers.set('cache-control', 'public, max-age=0, must-revalidate')

tests/e2e/page-router.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,21 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
590590
'getStaticProps duration should not be longer than 10 seconds',
591591
).toBeLessThan(10_000)
592592
})
593+
594+
test('API route calling res.revalidate() on page returning notFound: true is not cacheable', async ({
595+
page,
596+
pageRouter,
597+
}) => {
598+
// note: known conditions for problematic case is
599+
// 1. API route needs to call res.revalidate()
600+
// 2. revalidated page's getStaticProps must return notFound: true
601+
const response = await page.goto(
602+
new URL('/api/revalidate?path=/static/not-found', pageRouter.url).href,
603+
)
604+
605+
expect(response?.status()).toEqual(200)
606+
expect(response?.headers()['netlify-cdn-cache-control'] ?? '').not.toMatch(/(s-maxage|max-age)/)
607+
})
593608
})
594609

595610
test.describe('Page Router with basePath and i18n', () => {

0 commit comments

Comments
 (0)