@@ -11,14 +11,15 @@ import { type Span } from '@opentelemetry/api'
1111import type { PrerenderManifest } from 'next/dist/build/index.js'
1212import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
1313
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 ,
2223} from '../../shared/cache-types.cjs'
2324import { getRegionalBlobStore } from '../regional-blob-store.cjs'
2425
@@ -29,7 +30,7 @@ type TagManifest = { revalidatedAt: number }
2930
3031type TagManifestBlobCache = Record < string , Promise < TagManifest > >
3132
32- export class NetlifyCacheHandler implements CacheHandler {
33+ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
3334 options : CacheHandlerContext
3435 revalidatedTags : string [ ]
3536 blobStore : Store
@@ -132,13 +133,18 @@ export class NetlifyCacheHandler implements CacheHandler {
132133
133134 if (
134135 cacheValue . kind === 'PAGE' ||
136+ cacheValue . kind === 'PAGES' ||
135137 cacheValue . kind === 'APP_PAGE' ||
136- cacheValue . kind === 'ROUTE'
138+ cacheValue . kind === 'ROUTE' ||
139+ cacheValue . kind === 'APP_ROUTE'
137140 ) {
138141 if ( cacheValue . headers ?. [ NEXT_CACHE_TAGS_HEADER ] ) {
139142 const cacheTags = ( cacheValue . headers [ NEXT_CACHE_TAGS_HEADER ] as string ) . split ( ',' )
140143 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+ ) {
142148 // pages router doesn't have cache tags headers in PAGE cache value
143149 // so we need to generate appropriate cache tags for it
144150 const cacheTags = [ `_N_T_${ key === '/index' ? '/' : key } ` ]
@@ -185,7 +191,9 @@ export class NetlifyCacheHandler implements CacheHandler {
185191 }
186192 }
187193
188- async get ( ...args : Parameters < CacheHandler [ 'get' ] > ) : ReturnType < CacheHandler [ 'get' ] > {
194+ async get (
195+ ...args : Parameters < CacheHandlerForMultipleVersions [ 'get' ] >
196+ ) : ReturnType < CacheHandlerForMultipleVersions [ 'get' ] > {
189197 return this . tracer . withActiveSpan ( 'get cache key' , async ( span ) => {
190198 const [ key , ctx = { } ] = args
191199 getLogger ( ) . debug ( `[NetlifyCacheHandler.get]: ${ key } ` )
@@ -224,8 +232,12 @@ export class NetlifyCacheHandler implements CacheHandler {
224232 value : blob . value ,
225233 }
226234
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+ } )
229241
230242 const valueWithoutRevalidate = this . captureRouteRevalidateAndRemoveFromObject ( blob . value )
231243
@@ -237,8 +249,9 @@ export class NetlifyCacheHandler implements CacheHandler {
237249 } ,
238250 }
239251 }
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 } )
242255
243256 const { revalidate, ...restOfPageValue } = blob . value
244257
@@ -250,7 +263,7 @@ export class NetlifyCacheHandler implements CacheHandler {
250263 }
251264 }
252265 case 'APP_PAGE' : {
253- span . addEvent ( 'APP_PAGE' , { lastModified : blob . lastModified } )
266+ span . addEvent ( blob . value ?. kind , { lastModified : blob . lastModified } )
254267
255268 const { revalidate, rscData, ...restOfPageValue } = blob . value
256269
@@ -272,18 +285,22 @@ export class NetlifyCacheHandler implements CacheHandler {
272285 }
273286
274287 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 ] ,
277290 ) : NetlifyIncrementalCacheValue | null {
278- if ( data ?. kind === 'ROUTE' ) {
291+ if ( ! data ) {
292+ return null
293+ }
294+
295+ if ( isCachedRouteValue ( data ) ) {
279296 return {
280297 ...data ,
281298 revalidate : context . revalidate ,
282299 body : data . body . toString ( 'base64' ) ,
283300 }
284301 }
285302
286- if ( data ?. kind === 'PAGE' ) {
303+ if ( isCachedPageValue ( data ) ) {
287304 return {
288305 ...data ,
289306 revalidate : context . revalidate ,
@@ -301,7 +318,7 @@ export class NetlifyCacheHandler implements CacheHandler {
301318 return data
302319 }
303320
304- async set ( ...args : Parameters < IncrementalCache [ 'set' ] > ) {
321+ async set ( ...args : Parameters < CacheHandlerForMultipleVersions [ 'set' ] > ) {
305322 return this . tracer . withActiveSpan ( 'set cache key' , async ( span ) => {
306323 const [ key , data , context ] = args
307324 const blobKey = await this . encodeBlobKey ( key )
@@ -321,7 +338,7 @@ export class NetlifyCacheHandler implements CacheHandler {
321338 value,
322339 } )
323340
324- if ( data ?. kind === 'PAGE' ) {
341+ if ( data ?. kind === 'PAGE' || data ?. kind === 'PAGES' ) {
325342 const requestContext = getRequestContext ( )
326343 if ( requestContext ?. didPagesRouterOnDemandRevalidate ) {
327344 const tag = `_N_T_${ key === '/index' ? '/' : key } `
@@ -397,8 +414,10 @@ export class NetlifyCacheHandler implements CacheHandler {
397414 cacheTags = [ ...tags , ...softTags ]
398415 } else if (
399416 cacheEntry . value ?. kind === 'PAGE' ||
417+ cacheEntry . value ?. kind === 'PAGES' ||
400418 cacheEntry . value ?. kind === 'APP_PAGE' ||
401- cacheEntry . value ?. kind === 'ROUTE'
419+ cacheEntry . value ?. kind === 'ROUTE' ||
420+ cacheEntry . value ?. kind === 'APP_ROUTE'
402421 ) {
403422 cacheTags = ( cacheEntry . value . headers ?. [ NEXT_CACHE_TAGS_HEADER ] as string ) ?. split ( ',' ) || [ ]
404423 } else {
0 commit comments