Skip to content

Commit 50a930f

Browse files
committed
fix: use durable cache when serving stale response
1 parent 5a318ab commit 50a930f

File tree

5 files changed

+9
-9
lines changed

5 files changed

+9
-9
lines changed

src/run/headers.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ describe('headers', () => {
238238
expect(headers.set).toHaveBeenNthCalledWith(
239239
1,
240240
'netlify-cdn-cache-control',
241-
'public, max-age=0, must-revalidate',
241+
'public, max-age=0, must-revalidate, durable',
242242
)
243243
})
244244

@@ -257,7 +257,7 @@ describe('headers', () => {
257257
expect(headers.set).toHaveBeenNthCalledWith(
258258
1,
259259
'netlify-cdn-cache-control',
260-
'public, max-age=0, must-revalidate',
260+
'public, max-age=0, must-revalidate, durable',
261261
)
262262
})
263263

src/run/headers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export const setCacheControlHeaders = (
224224
const cdnCacheControl =
225225
// if we are serving already stale response, instruct edge to not attempt to cache that response
226226
headers.get('x-nextjs-cache') === 'STALE'
227-
? 'public, max-age=0, must-revalidate'
227+
? 'public, max-age=0, must-revalidate, durable'
228228
: `s-maxage=${requestContext.routeHandlerRevalidate === false ? 31536000 : requestContext.routeHandlerRevalidate}, stale-while-revalidate=31536000, durable`
229229

230230
headers.set('netlify-cdn-cache-control', cdnCacheControl)
@@ -246,7 +246,7 @@ export const setCacheControlHeaders = (
246246
const cdnCacheControl =
247247
// if we are serving already stale response, instruct edge to not attempt to cache that response
248248
headers.get('x-nextjs-cache') === 'STALE'
249-
? 'public, max-age=0, must-revalidate'
249+
? 'public, max-age=0, must-revalidate, durable'
250250
: [
251251
...getHeaderValueArray(cacheControl).map((value) =>
252252
value === 'stale-while-revalidate' ? 'stale-while-revalidate=31536000' : value,

tests/integration/cache-handler.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('page router', () => {
6565
expect(call1.headers, 'a stale page served with swr').toEqual(
6666
expect.objectContaining({
6767
'cache-status': '"Next.js"; hit; fwd=stale',
68-
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate',
68+
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate, durable',
6969
}),
7070
)
7171

@@ -223,7 +223,7 @@ describe('app router', () => {
223223
// It will be stale instead of hit
224224
expect.objectContaining({
225225
'cache-status': '"Next.js"; hit; fwd=stale',
226-
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate',
226+
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate, durable',
227227
}),
228228
)
229229
expect(

tests/integration/fetch-handler.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ test<FixtureTestContext>('if the fetch call is cached correctly (cached page res
195195
expect(post1.headers, 'a stale page served with swr').toEqual(
196196
expect.objectContaining({
197197
'cache-status': '"Next.js"; hit; fwd=stale',
198-
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate',
198+
'netlify-cdn-cache-control': 'public, max-age=0, must-revalidate, durable',
199199
}),
200200
)
201201

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

tests/integration/simple-app.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ test<FixtureTestContext>('cacheable route handler is cached on cdn (revalidate=1
198198
const firstTimeCachedResponse = await invokeFunction(ctx, { url: '/api/cached-revalidate' })
199199
// this will be "stale" response from build
200200
expect(firstTimeCachedResponse.headers['netlify-cdn-cache-control']).toBe(
201-
'public, max-age=0, must-revalidate',
201+
'public, max-age=0, must-revalidate, durable',
202202
)
203203

204204
// allow server to regenerate fresh response in background

0 commit comments

Comments
 (0)