@@ -5,7 +5,7 @@ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
5
5
import { type FixtureTestContext } from '../../tests/utils/contexts.js'
6
6
import { generateRandomObjectID , startMockBlobStore } from '../../tests/utils/helpers.js'
7
7
8
- import { createRequestContext } from './handlers/request-context.cjs'
8
+ import { createRequestContext , type RequestContext } from './handlers/request-context.cjs'
9
9
import { setCacheControlHeaders , setVaryHeaders } from './headers.js'
10
10
11
11
beforeEach < FixtureTestContext > ( async ( ctx ) => {
@@ -194,6 +194,133 @@ describe('headers', () => {
194
194
describe ( 'setCacheControlHeaders' , ( ) => {
195
195
const defaultUrl = 'https://example.com'
196
196
197
+ describe ( 'route handler responses with a specified `revalidate` value' , ( ) => {
198
+ test ( 'should not set any headers if "cdn-cache-control" is present' , ( ) => {
199
+ const givenHeaders = {
200
+ 'cdn-cache-control' : 'public, max-age=0, must-revalidate' ,
201
+ }
202
+ const headers = new Headers ( givenHeaders )
203
+ const request = new Request ( defaultUrl )
204
+ vi . spyOn ( headers , 'set' )
205
+
206
+ const ctx : RequestContext = { ...createRequestContext ( ) , routeHandlerRevalidate : false }
207
+ setCacheControlHeaders ( headers , request , ctx )
208
+
209
+ expect ( headers . set ) . toHaveBeenCalledTimes ( 0 )
210
+ } )
211
+
212
+ test ( 'should not set any headers if "netlify-cdn-cache-control" is present' , ( ) => {
213
+ const givenHeaders = {
214
+ 'netlify-cdn-cache-control' : 'public, max-age=0, must-revalidate' ,
215
+ }
216
+ const headers = new Headers ( givenHeaders )
217
+ const request = new Request ( defaultUrl )
218
+ vi . spyOn ( headers , 'set' )
219
+
220
+ const ctx : RequestContext = { ...createRequestContext ( ) , routeHandlerRevalidate : false }
221
+ setCacheControlHeaders ( headers , request , ctx )
222
+
223
+ expect ( headers . set ) . toHaveBeenCalledTimes ( 0 )
224
+ } )
225
+
226
+ test ( 'should mark content as stale if "{netlify-,}cdn-cache-control" is not present and "x-nextjs-cache" is "STALE" (GET)' , ( ) => {
227
+ const givenHeaders = {
228
+ 'x-nextjs-cache' : 'STALE' ,
229
+ }
230
+ const headers = new Headers ( givenHeaders )
231
+ const request = new Request ( defaultUrl )
232
+ vi . spyOn ( headers , 'set' )
233
+
234
+ const ctx : RequestContext = { ...createRequestContext ( ) , routeHandlerRevalidate : false }
235
+ setCacheControlHeaders ( headers , request , ctx )
236
+
237
+ expect ( headers . set ) . toHaveBeenCalledTimes ( 1 )
238
+ expect ( headers . set ) . toHaveBeenNthCalledWith (
239
+ 1 ,
240
+ 'netlify-cdn-cache-control' ,
241
+ 'public, max-age=0, must-revalidate' ,
242
+ )
243
+ } )
244
+
245
+ test ( 'should mark content as stale if "{netlify-,}cdn-cache-control" is not present and "x-nextjs-cache" is "STALE" (HEAD)' , ( ) => {
246
+ const givenHeaders = {
247
+ 'x-nextjs-cache' : 'STALE' ,
248
+ }
249
+ const headers = new Headers ( givenHeaders )
250
+ const request = new Request ( defaultUrl )
251
+ vi . spyOn ( headers , 'set' )
252
+
253
+ const ctx : RequestContext = { ...createRequestContext ( ) , routeHandlerRevalidate : false }
254
+ setCacheControlHeaders ( headers , request , ctx )
255
+
256
+ expect ( headers . set ) . toHaveBeenCalledTimes ( 1 )
257
+ expect ( headers . set ) . toHaveBeenNthCalledWith (
258
+ 1 ,
259
+ 'netlify-cdn-cache-control' ,
260
+ 'public, max-age=0, must-revalidate' ,
261
+ )
262
+ } )
263
+
264
+ test ( 'should set durable SWC=1yr with 1yr TTL if "{netlify-,}cdn-cache-control" is not present and `revalidate` is `false` (HEAD)' , ( ) => {
265
+ const headers = new Headers ( )
266
+ const request = new Request ( defaultUrl , { method : 'HEAD' } )
267
+ vi . spyOn ( headers , 'set' )
268
+
269
+ const ctx : RequestContext = { ...createRequestContext ( ) , routeHandlerRevalidate : false }
270
+ setCacheControlHeaders ( headers , request , ctx )
271
+
272
+ expect ( headers . set ) . toHaveBeenCalledTimes ( 1 )
273
+ expect ( headers . set ) . toHaveBeenNthCalledWith (
274
+ 1 ,
275
+ 'netlify-cdn-cache-control' ,
276
+ 's-maxage=31536000, stale-while-revalidate=31536000, durable' ,
277
+ )
278
+ } )
279
+
280
+ test ( 'should set durable SWC=1yr with given TTL if "{netlify-,}cdn-cache-control" is not present and `revalidate` is a number (GET)' , ( ) => {
281
+ const headers = new Headers ( )
282
+ const request = new Request ( defaultUrl )
283
+ vi . spyOn ( headers , 'set' )
284
+
285
+ const ctx : RequestContext = { ...createRequestContext ( ) , routeHandlerRevalidate : 7200 }
286
+ setCacheControlHeaders ( headers , request , ctx )
287
+
288
+ expect ( headers . set ) . toHaveBeenCalledTimes ( 1 )
289
+ expect ( headers . set ) . toHaveBeenNthCalledWith (
290
+ 1 ,
291
+ 'netlify-cdn-cache-control' ,
292
+ 's-maxage=7200, stale-while-revalidate=31536000, durable' ,
293
+ )
294
+ } )
295
+
296
+ test ( 'should set durable SWC=1yr with 1yr TTL if "{netlify-,}cdn-cache-control" is not present and `revalidate` is a number (HEAD)' , ( ) => {
297
+ const headers = new Headers ( )
298
+ const request = new Request ( defaultUrl , { method : 'HEAD' } )
299
+ vi . spyOn ( headers , 'set' )
300
+
301
+ const ctx : RequestContext = { ...createRequestContext ( ) , routeHandlerRevalidate : 7200 }
302
+ setCacheControlHeaders ( headers , request , ctx )
303
+
304
+ expect ( headers . set ) . toHaveBeenCalledTimes ( 1 )
305
+ expect ( headers . set ) . toHaveBeenNthCalledWith (
306
+ 1 ,
307
+ 'netlify-cdn-cache-control' ,
308
+ 's-maxage=7200, stale-while-revalidate=31536000, durable' ,
309
+ )
310
+ } )
311
+
312
+ test ( 'should not set any headers on POST request' , ( ) => {
313
+ const headers = new Headers ( )
314
+ const request = new Request ( defaultUrl , { method : 'POST' } )
315
+ vi . spyOn ( headers , 'set' )
316
+
317
+ const ctx : RequestContext = { ...createRequestContext ( ) , routeHandlerRevalidate : false }
318
+ setCacheControlHeaders ( headers , request , ctx )
319
+
320
+ expect ( headers . set ) . toHaveBeenCalledTimes ( 0 )
321
+ } )
322
+ } )
323
+
197
324
test ( 'should not set any headers if "cache-control" is not set and "requestContext.usedFsRead" is not truthy' , ( ) => {
198
325
const headers = new Headers ( )
199
326
const request = new Request ( defaultUrl )
@@ -347,7 +474,7 @@ describe('headers', () => {
347
474
)
348
475
} )
349
476
350
- test ( 'should set default "cache-control" header if it contains only "s-maxage" and "stale-whie -revalidate"' , ( ) => {
477
+ test ( 'should set default "cache-control" header if it contains only "s-maxage" and "stale-while -revalidate"' , ( ) => {
351
478
const givenHeaders = {
352
479
'cache-control' : 's-maxage=604800, stale-while-revalidate=86400' ,
353
480
}
0 commit comments