@@ -121,11 +121,8 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
121
121
}
122
122
123
123
private captureCacheTags ( cacheValue : NetlifyIncrementalCacheValue | null , key : string ) {
124
- if ( ! cacheValue ) {
125
- return
126
- }
127
-
128
124
const requestContext = getRequestContext ( )
125
+
129
126
// Bail if we can't get request context
130
127
if ( ! requestContext ) {
131
128
return
@@ -141,6 +138,13 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
141
138
return
142
139
}
143
140
141
+ // Set cache tags for 404 pages as well so that the content can later be purged
142
+ if ( ! cacheValue ) {
143
+ const cacheTags = [ `_N_T_${ key === '/index' ? '/' : encodeURI ( key ) } ` ]
144
+ requestContext . responseCacheTags = cacheTags
145
+ return
146
+ }
147
+
144
148
if (
145
149
cacheValue . kind === 'PAGE' ||
146
150
cacheValue . kind === 'PAGES' ||
@@ -226,7 +230,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
226
230
...args : Parameters < CacheHandlerForMultipleVersions [ 'get' ] >
227
231
) : ReturnType < CacheHandlerForMultipleVersions [ 'get' ] > {
228
232
return this . tracer . withActiveSpan ( 'get cache key' , async ( span ) => {
229
- const [ key , ctx = { } ] = args
233
+ const [ key , context = { } ] = args
230
234
getLogger ( ) . debug ( `[NetlifyCacheHandler.get]: ${ key } ` )
231
235
232
236
span . setAttributes ( { key } )
@@ -259,21 +263,30 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
259
263
return null
260
264
}
261
265
262
- const staleByTags = await this . checkCacheEntryStaleByTags ( blob , ctx . tags , ctx . softTags )
266
+ const staleByTags = await this . checkCacheEntryStaleByTags (
267
+ blob ,
268
+ context . tags ,
269
+ context . softTags ,
270
+ )
263
271
264
272
if ( staleByTags ) {
265
273
span . addEvent ( 'Stale' , { staleByTags, key, ttl } )
266
274
return null
267
275
}
268
276
269
277
this . captureResponseCacheLastModified ( blob , key , span )
270
- this . captureCacheTags ( blob . value , key )
278
+
279
+ // Next sets a kind/kindHint and fetchUrl for data requests, however fetchUrl was found to be most reliable across versions
280
+ const isDataRequest = Boolean ( context . fetchUrl )
281
+ if ( ! isDataRequest ) {
282
+ this . captureCacheTags ( blob . value , key )
283
+ }
271
284
272
285
switch ( blob . value ?. kind ) {
273
286
case 'FETCH' :
274
287
span . addEvent ( 'FETCH' , {
275
288
lastModified : blob . lastModified ,
276
- revalidate : ctx . revalidate ,
289
+ revalidate : context . revalidate ,
277
290
ttl,
278
291
} )
279
292
return {
@@ -387,13 +400,17 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
387
400
388
401
const value = this . transformToStorableObject ( data , context )
389
402
390
- // if previous CacheHandler.get call returned null (page was either never rendered or was on-demand revalidated)
391
- // and we didn't yet capture cache tags, we try to get cache tags from freshly produced cache value
392
- this . captureCacheTags ( value , key )
403
+ // Next sets a fetchCache and fetchUrl for data requests, however fetchUrl was found to be most reliable across versions
404
+ const isDataReq = Boolean ( context . fetchUrl )
405
+ if ( ! isDataReq ) {
406
+ // if previous CacheHandler.get call returned null (page was either never rendered or was on-demand revalidated)
407
+ // and we didn't yet capture cache tags, we try to get cache tags from freshly produced cache value
408
+ this . captureCacheTags ( value , key )
409
+ }
393
410
394
411
await this . cacheStore . set ( key , { lastModified, value } , 'blobStore.set' )
395
412
396
- if ( data ?. kind === 'PAGE' || data ?. kind === 'PAGES' ) {
413
+ if ( ( ! data && ! isDataReq ) || data ?. kind === 'PAGE' || data ?. kind === 'PAGES' ) {
397
414
const requestContext = getRequestContext ( )
398
415
if ( requestContext ?. didPagesRouterOnDemandRevalidate ) {
399
416
// encode here to deal with non ASCII characters in the key
0 commit comments