Skip to content

Commit

Permalink
fix: use durable cache when serving stale response (#2591)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh authored Sep 17, 2024
1 parent 220e17f commit 79e2a2c
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
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

0 comments on commit 79e2a2c

Please sign in to comment.