1
+ import path from "path" ;
1
2
import {
2
3
BatchWriteItemCommand ,
3
4
DynamoDBClient ,
@@ -12,15 +13,8 @@ import {
12
13
PutObjectCommandOutput ,
13
14
S3Client ,
14
15
} from "@aws-sdk/client-s3" ;
15
- // //@ts -ignore
16
- // import { getDerivedTags } from "next/dist/server/lib/incremental-cache/utils";
17
- import path from "path" ;
18
-
19
16
import { debug , error } from "./logger.js" ;
20
17
21
- // TODO: Remove this, temporary only to run some tests
22
- const getDerivedTags = ( tags : string [ ] ) => tags ;
23
-
24
18
interface CachedFetchValue {
25
19
kind : "FETCH" ;
26
20
data : {
@@ -146,7 +140,7 @@ export default class S3Cache {
146
140
const { Body, LastModified } = await this . getS3Object ( key , "fetch" , keys ) ;
147
141
const lastModified = await this . getHasRevalidatedTags (
148
142
key ,
149
- LastModified ?. getTime ( ) ,
143
+ LastModified ?. getTime ( )
150
144
) ;
151
145
if ( lastModified === - 1 ) {
152
146
// If some tags are stale we need to force revalidation
@@ -167,7 +161,7 @@ export default class S3Cache {
167
161
168
162
async getIncrementalCache (
169
163
key : string ,
170
- keys : string [ ] ,
164
+ keys : string [ ]
171
165
) : Promise < CacheHandlerValue | null > {
172
166
if ( keys . includes ( this . buildS3Key ( key , "body" ) ) ) {
173
167
debug ( "get body cache " , { key } ) ;
@@ -209,7 +203,7 @@ export default class S3Cache {
209
203
] ) ;
210
204
const lastModified = await this . getHasRevalidatedTags (
211
205
key ,
212
- LastModified ?. getTime ( ) ,
206
+ LastModified ?. getTime ( )
213
207
) ;
214
208
if ( lastModified === - 1 ) {
215
209
// If some tags are stale we need to force revalidation
@@ -242,7 +236,7 @@ export default class S3Cache {
242
236
const { Body, LastModified } = await this . getS3Object (
243
237
key ,
244
238
"redirect" ,
245
- keys ,
239
+ keys
246
240
) ;
247
241
return {
248
242
lastModified : LastModified ?. getTime ( ) ,
@@ -276,15 +270,15 @@ export default class S3Cache {
276
270
metaPromise = this . putS3Object (
277
271
key ,
278
272
"meta" ,
279
- JSON . stringify ( { status : data . status , headers : data . headers } ) ,
273
+ JSON . stringify ( { status : data . status , headers : data . headers } )
280
274
) ;
281
275
}
282
276
await Promise . all ( [
283
277
this . putS3Object ( key , "html" , html ) ,
284
278
this . putS3Object (
285
279
key ,
286
280
isAppPath ? "rsc" : "json" ,
287
- isAppPath ? pageData : JSON . stringify ( pageData ) ,
281
+ isAppPath ? pageData : JSON . stringify ( pageData )
288
282
) ,
289
283
metaPromise ,
290
284
] ) ;
@@ -297,13 +291,13 @@ export default class S3Cache {
297
291
} else if ( data === null || data === undefined ) {
298
292
await this . deleteS3Objects ( key ) ;
299
293
}
300
- // Write getDerivedTags to dynamodb
294
+ // Write derivedTags to dynamodb
301
295
// If we use an in house version of getDerivedTags in build we should use it here instead of next's one
302
296
const derivedTags : string [ ] =
303
297
data ?. kind === "FETCH"
304
- ? getDerivedTags ( data . data . tags ?? [ ] )
298
+ ? data . data . tags ?? [ ]
305
299
: data ?. kind === "PAGE"
306
- ? getDerivedTags ( data . headers ?. [ "x-next-cache-tags" ] ?. split ( "," ) ?? [ ] )
300
+ ? data . headers ?. [ "x-next-cache-tags" ] ?. split ( "," ) ?? [ ]
307
301
: [ ] ;
308
302
debug ( "derivedTags" , derivedTags ) ;
309
303
// Get all tags stored in dynamodb for the given key
@@ -315,7 +309,7 @@ export default class S3Cache {
315
309
tagsToWrite . map ( ( tag ) => ( {
316
310
path : key ,
317
311
tag : tag ,
318
- } ) ) ,
312
+ } ) )
319
313
) ;
320
314
}
321
315
}
@@ -333,7 +327,7 @@ export default class S3Cache {
333
327
paths ?. map ( ( path ) => ( {
334
328
path : path ,
335
329
tag : tag ,
336
- } ) ) ?? [ ] ,
330
+ } ) ) ?? [ ]
337
331
) ;
338
332
}
339
333
@@ -353,7 +347,7 @@ export default class S3Cache {
353
347
ExpressionAttributeValues : {
354
348
":key" : { S : this . buildDynamoKey ( path ) } ,
355
349
} ,
356
- } ) ,
350
+ } )
357
351
) ;
358
352
const tags = result . Items ?. map ( ( item ) => item . tag . S ?? "" ) ?? [ ] ;
359
353
debug ( "tags for path" , path , tags ) ;
@@ -382,7 +376,7 @@ export default class S3Cache {
382
376
":key" : { S : this . buildDynamoKey ( key ) } ,
383
377
":lastModified" : { N : String ( lastModified ?? 0 ) } ,
384
378
} ,
385
- } ) ,
379
+ } )
386
380
) ;
387
381
const revalidatedTags = result . Items ?? [ ] ;
388
382
debug ( "revalidatedTags" , revalidatedTags ) ;
@@ -407,12 +401,12 @@ export default class S3Cache {
407
401
ExpressionAttributeValues : {
408
402
":tag" : { S : this . buildDynamoKey ( tag ) } ,
409
403
} ,
410
- } ) ,
404
+ } )
411
405
) ;
412
406
return (
413
407
// We need to remove the buildId from the path
414
408
Items ?. map (
415
- ( { path : { S : key } } ) => key ?. replace ( `${ this . buildId } /` , "" ) ?? "" ,
409
+ ( { path : { S : key } } ) => key ?. replace ( `${ this . buildId } /` , "" ) ?? ""
416
410
) ?? [ ]
417
411
) ;
418
412
} catch ( e ) {
@@ -437,9 +431,9 @@ export default class S3Cache {
437
431
} ,
438
432
} ) ) ,
439
433
} ,
440
- } ) ,
434
+ } )
441
435
) ;
442
- } ) ,
436
+ } )
443
437
) ;
444
438
} catch ( e ) {
445
439
error ( "Failed to batch write dynamo item" , e ) ;
@@ -475,7 +469,7 @@ export default class S3Cache {
475
469
CACHE_BUCKET_KEY_PREFIX ?? "" ,
476
470
extension === "fetch" ? "__fetch" : "" ,
477
471
this . buildId ,
478
- extension === "fetch" ? key : `${ key } .${ extension } ` ,
472
+ extension === "fetch" ? key : `${ key } .${ extension } `
479
473
) ;
480
474
}
481
475
@@ -490,7 +484,7 @@ export default class S3Cache {
490
484
// add a point to the key so that it only matches the key and
491
485
// not other keys starting with the same string
492
486
Prefix : `${ this . buildS3KeyPrefix ( key ) } .` ,
493
- } ) ,
487
+ } )
494
488
) ;
495
489
return ( Contents ?? [ ] ) . map ( ( { Key } ) => Key ) as string [ ] ;
496
490
}
@@ -503,7 +497,7 @@ export default class S3Cache {
503
497
new GetObjectCommand ( {
504
498
Bucket : CACHE_BUCKET_NAME ,
505
499
Key : this . buildS3Key ( key , extension ) ,
506
- } ) ,
500
+ } )
507
501
) ;
508
502
return result ;
509
503
} catch ( e ) {
@@ -515,22 +509,22 @@ export default class S3Cache {
515
509
private putS3Object (
516
510
key : string ,
517
511
extension : Extension ,
518
- value : PutObjectCommandInput [ "Body" ] ,
512
+ value : PutObjectCommandInput [ "Body" ]
519
513
) {
520
514
return this . client . send (
521
515
new PutObjectCommand ( {
522
516
Bucket : CACHE_BUCKET_NAME ,
523
517
Key : this . buildS3Key ( key , extension ) ,
524
518
Body : value ,
525
- } ) ,
519
+ } )
526
520
) ;
527
521
}
528
522
529
523
private async deleteS3Objects ( key : string ) {
530
524
try {
531
525
const regex = new RegExp ( `\.(json|rsc|html|body|meta|fetch|redirect)$` ) ;
532
526
const s3Keys = ( await this . listS3Object ( key ) ) . filter (
533
- ( key ) => key && regex . test ( key ) ,
527
+ ( key ) => key && regex . test ( key )
534
528
) ;
535
529
536
530
await this . client . send (
@@ -539,7 +533,7 @@ export default class S3Cache {
539
533
Delete : {
540
534
Objects : s3Keys . map ( ( Key ) => ( { Key } ) ) ,
541
535
} ,
542
- } ) ,
536
+ } )
543
537
) ;
544
538
} catch ( e ) {
545
539
error ( "Failed to delete cache" , e ) ;
0 commit comments