Skip to content

fix: use durable cache for stale responses #2591

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 1 commit into from
Sep 17, 2024
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
4 changes: 2 additions & 2 deletions src/run/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ describe('headers', () => {
expect(headers.set).toHaveBeenNthCalledWith(
1,
'netlify-cdn-cache-control',
'public, max-age=0, must-revalidate',
'public, max-age=0, must-revalidate, durable',
)
})

Expand All @@ -257,7 +257,7 @@ describe('headers', () => {
expect(headers.set).toHaveBeenNthCalledWith(
1,
'netlify-cdn-cache-control',
'public, max-age=0, must-revalidate',
'public, max-age=0, must-revalidate, durable',
)
})

Expand Down
4 changes: 2 additions & 2 deletions src/run/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export const setCacheControlHeaders = (
const cdnCacheControl =
// if we are serving already stale response, instruct edge to not attempt to cache that response
headers.get('x-nextjs-cache') === 'STALE'
? 'public, max-age=0, must-revalidate'
? 'public, max-age=0, must-revalidate, durable'
: `s-maxage=${requestContext.routeHandlerRevalidate === false ? 31536000 : requestContext.routeHandlerRevalidate}, stale-while-revalidate=31536000, durable`

headers.set('netlify-cdn-cache-control', cdnCacheControl)
Expand All @@ -246,7 +246,7 @@ export const setCacheControlHeaders = (
const cdnCacheControl =
// if we are serving already stale response, instruct edge to not attempt to cache that response
headers.get('x-nextjs-cache') === 'STALE'
? 'public, max-age=0, must-revalidate'
? 'public, max-age=0, must-revalidate, durable'
: [
...getHeaderValueArray(cacheControl).map((value) =>
value === 'stale-while-revalidate' ? 'stale-while-revalidate=31536000' : value,
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/cache-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('page router', () => {
expect(call1.headers, 'a stale page served with swr').toEqual(
expect.objectContaining({
'cache-status': '"Next.js"; hit; fwd=stale',
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate',
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate, durable',
}),
)

Expand Down Expand Up @@ -223,7 +223,7 @@ describe('app router', () => {
// It will be stale instead of hit
expect.objectContaining({
'cache-status': '"Next.js"; hit; fwd=stale',
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate',
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate, durable',
}),
)
expect(
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/fetch-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ test<FixtureTestContext>('if the fetch call is cached correctly (cached page res
expect(post1.headers, 'a stale page served with swr').toEqual(
expect.objectContaining({
'cache-status': '"Next.js"; hit; fwd=stale',
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate',
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate, durable',
}),
)

Expand Down Expand Up @@ -264,7 +264,7 @@ test<FixtureTestContext>('if the fetch call is cached correctly (cached page res
expect(post3.headers, 'a stale page served with swr').toEqual(
expect.objectContaining({
'cache-status': '"Next.js"; hit; fwd=stale',
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate',
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate, durable',
}),
)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/simple-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ test<FixtureTestContext>('cacheable route handler is cached on cdn (revalidate=1
const firstTimeCachedResponse = await invokeFunction(ctx, { url: '/api/cached-revalidate' })
// this will be "stale" response from build
expect(firstTimeCachedResponse.headers['netlify-cdn-cache-control']).toBe(
'public, max-age=0, must-revalidate',
'public, max-age=0, must-revalidate, durable',
)

// allow server to regenerate fresh response in background
Expand Down
Loading