Skip to content

fix: don't set permanent caching header when res.revalidate() was used #2810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/run/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/page-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading