1
- import { readFileSync } from "node:fs" ;
1
+ import { existsSync , readFileSync } from "node:fs" ;
2
2
import path from "node:path" ;
3
3
4
4
import type { BuildOptions } from "@opennextjs/aws/build/helper.js" ;
5
- import { getBuildId } from "@opennextjs/aws/build/helper.js" ;
6
- import { globSync } from "glob" ;
7
-
8
- type CacheInfo = {
9
- type : string ;
10
- meta ?: {
11
- headers ?: {
12
- "x-next-cache-tags" ?: string ;
13
- } ;
14
- } ;
15
- } ;
5
+
6
+ type RawManifest = {
7
+ tag : { S : string } ;
8
+ path : { S : string } ;
9
+ revalidatedAt : { N : string } ;
10
+ } [ ] ;
16
11
17
12
export type CacheAssetsManifest = {
18
13
tags : { [ tag : string ] : string [ ] } ;
@@ -24,25 +19,28 @@ export type CacheAssetsManifest = {
24
19
*
25
20
* The mapping creates an index of each tags pointing to its paths, and each path pointing to its tags.
26
21
*/
27
- export function extractCacheAssetsManifest ( buildOpts : BuildOptions ) {
28
- const cachePath = path . join ( buildOpts . outputDir , "cache" ) ;
29
- const buildId = getBuildId ( buildOpts ) ;
30
-
31
- const manifest = globSync ( path . join ( cachePath , buildId , "**/*.cache" ) ) . reduce < CacheAssetsManifest > (
32
- ( acc , p ) => {
33
- const { meta } = JSON . parse ( readFileSync ( p , "utf-8" ) ) as CacheInfo ;
34
- const tags = meta ?. headers ?. [ "x-next-cache-tags" ] ?. split ( "," ) ?. map ( ( tag ) => `${ buildId } /${ tag } ` ) ?? [ ] ;
35
-
36
- const routePath = path . relative ( cachePath , p ) . replace ( / \. c a c h e $ / , "" ) ;
37
-
38
- acc . paths [ routePath ] = tags ;
22
+ export function extractCacheAssetsManifest ( options : BuildOptions ) : CacheAssetsManifest {
23
+ // TODO: Expose the function for getting this data as an adapter-agnostic utility in AWS.
24
+ const rawManifestPath = path . join ( options . outputDir , "dynamodb-provider" , "dynamodb-cache.json" ) ;
25
+
26
+ if ( ! existsSync ( rawManifestPath ) ) {
27
+ return { tags : { } , paths : { } } ;
28
+ }
29
+
30
+ const rawManifest : RawManifest = JSON . parse ( readFileSync ( rawManifestPath , "utf-8" ) ) ;
31
+
32
+ const manifest = rawManifest . reduce < CacheAssetsManifest > (
33
+ ( acc , { tag : { S : tag } , path : { S : path } } ) => {
34
+ if ( ! acc . paths [ path ] ) {
35
+ acc . paths [ path ] = [ tag ] ;
36
+ } else {
37
+ acc . paths [ path ] . push ( tag ) ;
38
+ }
39
39
40
- for ( const tag of tags ) {
41
- if ( ! acc . tags [ tag ] ) {
42
- acc . tags [ tag ] = [ routePath ] ;
43
- } else {
44
- acc . tags [ tag ] . push ( routePath ) ;
45
- }
40
+ if ( ! acc . tags [ tag ] ) {
41
+ acc . tags [ tag ] = [ path ] ;
42
+ } else {
43
+ acc . tags [ tag ] . push ( path ) ;
46
44
}
47
45
48
46
return acc ;
0 commit comments