@@ -11,14 +11,15 @@ import { type Span } from '@opentelemetry/api'
11
11
import type { PrerenderManifest } from 'next/dist/build/index.js'
12
12
import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
13
13
14
- import type {
15
- CacheHandler ,
16
- CacheHandlerContext ,
17
- IncrementalCache ,
18
- NetlifyCachedPageValue ,
19
- NetlifyCachedRouteValue ,
20
- NetlifyCacheHandlerValue ,
21
- NetlifyIncrementalCacheValue ,
14
+ import {
15
+ type CacheHandlerContext ,
16
+ type CacheHandlerForMultipleVersions ,
17
+ isCachedPageValue ,
18
+ isCachedRouteValue ,
19
+ type NetlifyCachedPageValue ,
20
+ type NetlifyCachedRouteValue ,
21
+ type NetlifyCacheHandlerValue ,
22
+ type NetlifyIncrementalCacheValue ,
22
23
} from '../../shared/cache-types.cjs'
23
24
import { getRegionalBlobStore } from '../regional-blob-store.cjs'
24
25
@@ -29,7 +30,7 @@ type TagManifest = { revalidatedAt: number }
29
30
30
31
type TagManifestBlobCache = Record < string , Promise < TagManifest > >
31
32
32
- export class NetlifyCacheHandler implements CacheHandler {
33
+ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
33
34
options : CacheHandlerContext
34
35
revalidatedTags : string [ ]
35
36
blobStore : Store
@@ -132,13 +133,18 @@ export class NetlifyCacheHandler implements CacheHandler {
132
133
133
134
if (
134
135
cacheValue . kind === 'PAGE' ||
136
+ cacheValue . kind === 'PAGES' ||
135
137
cacheValue . kind === 'APP_PAGE' ||
136
- cacheValue . kind === 'ROUTE'
138
+ cacheValue . kind === 'ROUTE' ||
139
+ cacheValue . kind === 'APP_ROUTE'
137
140
) {
138
141
if ( cacheValue . headers ?. [ NEXT_CACHE_TAGS_HEADER ] ) {
139
142
const cacheTags = ( cacheValue . headers [ NEXT_CACHE_TAGS_HEADER ] as string ) . split ( ',' )
140
143
requestContext . responseCacheTags = cacheTags
141
- } else if ( cacheValue . kind === 'PAGE' && typeof cacheValue . pageData === 'object' ) {
144
+ } else if (
145
+ ( cacheValue . kind === 'PAGE' || cacheValue . kind === 'PAGES' ) &&
146
+ typeof cacheValue . pageData === 'object'
147
+ ) {
142
148
// pages router doesn't have cache tags headers in PAGE cache value
143
149
// so we need to generate appropriate cache tags for it
144
150
const cacheTags = [ `_N_T_${ key === '/index' ? '/' : key } ` ]
@@ -185,7 +191,9 @@ export class NetlifyCacheHandler implements CacheHandler {
185
191
}
186
192
}
187
193
188
- async get ( ...args : Parameters < CacheHandler [ 'get' ] > ) : ReturnType < CacheHandler [ 'get' ] > {
194
+ async get (
195
+ ...args : Parameters < CacheHandlerForMultipleVersions [ 'get' ] >
196
+ ) : ReturnType < CacheHandlerForMultipleVersions [ 'get' ] > {
189
197
return this . tracer . withActiveSpan ( 'get cache key' , async ( span ) => {
190
198
const [ key , ctx = { } ] = args
191
199
getLogger ( ) . debug ( `[NetlifyCacheHandler.get]: ${ key } ` )
@@ -224,8 +232,12 @@ export class NetlifyCacheHandler implements CacheHandler {
224
232
value : blob . value ,
225
233
}
226
234
227
- case 'ROUTE' : {
228
- span . addEvent ( 'ROUTE' , { lastModified : blob . lastModified , status : blob . value . status } )
235
+ case 'ROUTE' :
236
+ case 'APP_ROUTE' : {
237
+ span . addEvent ( blob . value ?. kind , {
238
+ lastModified : blob . lastModified ,
239
+ status : blob . value . status ,
240
+ } )
229
241
230
242
const valueWithoutRevalidate = this . captureRouteRevalidateAndRemoveFromObject ( blob . value )
231
243
@@ -237,8 +249,9 @@ export class NetlifyCacheHandler implements CacheHandler {
237
249
} ,
238
250
}
239
251
}
240
- case 'PAGE' : {
241
- span . addEvent ( 'PAGE' , { lastModified : blob . lastModified } )
252
+ case 'PAGE' :
253
+ case 'PAGES' : {
254
+ span . addEvent ( blob . value ?. kind , { lastModified : blob . lastModified } )
242
255
243
256
const { revalidate, ...restOfPageValue } = blob . value
244
257
@@ -250,7 +263,7 @@ export class NetlifyCacheHandler implements CacheHandler {
250
263
}
251
264
}
252
265
case 'APP_PAGE' : {
253
- span . addEvent ( 'APP_PAGE' , { lastModified : blob . lastModified } )
266
+ span . addEvent ( blob . value ?. kind , { lastModified : blob . lastModified } )
254
267
255
268
const { revalidate, rscData, ...restOfPageValue } = blob . value
256
269
@@ -272,18 +285,22 @@ export class NetlifyCacheHandler implements CacheHandler {
272
285
}
273
286
274
287
private transformToStorableObject (
275
- data : Parameters < IncrementalCache [ 'set' ] > [ 1 ] ,
276
- context : Parameters < IncrementalCache [ 'set' ] > [ 2 ] ,
288
+ data : Parameters < CacheHandlerForMultipleVersions [ 'set' ] > [ 1 ] ,
289
+ context : Parameters < CacheHandlerForMultipleVersions [ 'set' ] > [ 2 ] ,
277
290
) : NetlifyIncrementalCacheValue | null {
278
- if ( data ?. kind === 'ROUTE' ) {
291
+ if ( ! data ) {
292
+ return null
293
+ }
294
+
295
+ if ( isCachedRouteValue ( data ) ) {
279
296
return {
280
297
...data ,
281
298
revalidate : context . revalidate ,
282
299
body : data . body . toString ( 'base64' ) ,
283
300
}
284
301
}
285
302
286
- if ( data ?. kind === 'PAGE' ) {
303
+ if ( isCachedPageValue ( data ) ) {
287
304
return {
288
305
...data ,
289
306
revalidate : context . revalidate ,
@@ -301,7 +318,7 @@ export class NetlifyCacheHandler implements CacheHandler {
301
318
return data
302
319
}
303
320
304
- async set ( ...args : Parameters < IncrementalCache [ 'set' ] > ) {
321
+ async set ( ...args : Parameters < CacheHandlerForMultipleVersions [ 'set' ] > ) {
305
322
return this . tracer . withActiveSpan ( 'set cache key' , async ( span ) => {
306
323
const [ key , data , context ] = args
307
324
const blobKey = await this . encodeBlobKey ( key )
@@ -321,7 +338,7 @@ export class NetlifyCacheHandler implements CacheHandler {
321
338
value,
322
339
} )
323
340
324
- if ( data ?. kind === 'PAGE' ) {
341
+ if ( data ?. kind === 'PAGE' || data ?. kind === 'PAGES' ) {
325
342
const requestContext = getRequestContext ( )
326
343
if ( requestContext ?. didPagesRouterOnDemandRevalidate ) {
327
344
const tag = `_N_T_${ key === '/index' ? '/' : key } `
@@ -397,8 +414,10 @@ export class NetlifyCacheHandler implements CacheHandler {
397
414
cacheTags = [ ...tags , ...softTags ]
398
415
} else if (
399
416
cacheEntry . value ?. kind === 'PAGE' ||
417
+ cacheEntry . value ?. kind === 'PAGES' ||
400
418
cacheEntry . value ?. kind === 'APP_PAGE' ||
401
- cacheEntry . value ?. kind === 'ROUTE'
419
+ cacheEntry . value ?. kind === 'ROUTE' ||
420
+ cacheEntry . value ?. kind === 'APP_ROUTE'
402
421
) {
403
422
cacheTags = ( cacheEntry . value . headers ?. [ NEXT_CACHE_TAGS_HEADER ] as string ) ?. split ( ',' ) || [ ]
404
423
} else {
0 commit comments