1
- import { appendFileSync , existsSync , mkdirSync , readFileSync , writeFileSync } from "node:fs" ;
1
+ import { appendFileSync , mkdirSync , writeFileSync } from "node:fs" ;
2
2
import path from "node:path" ;
3
3
4
4
import type { BuildOptions } from "@opennextjs/aws/build/helper.js" ;
5
-
6
- type RawManifest = {
7
- tag : { S : string } ;
8
- path : { S : string } ;
9
- revalidatedAt : { N : string } ;
10
- } [ ] ;
5
+ import type { TagCacheMetaFile } from "@opennextjs/aws/types/open-next.js" ;
11
6
12
7
/**
13
8
* Generates SQL statements that can be used to initialise the cache assets manifest in an SQL data store.
14
9
*/
15
- export function compileCacheAssetsManifestSqlFile ( options : BuildOptions ) {
16
- // TODO: Expose the function for getting this data as an adapter-agnostic utility in AWS.
17
- const rawManifestPath = path . join ( options . outputDir , "dynamodb-provider/dynamodb-cache.json" ) ;
10
+ export function compileCacheAssetsManifestSqlFile ( options : BuildOptions , metaFiles : TagCacheMetaFile [ ] ) {
18
11
const outputPath = path . join ( options . outputDir , "cloudflare/cache-assets-manifest.sql" ) ;
19
12
20
13
const tagsTable = process . env . NEXT_CACHE_D1_TAGS_TABLE || "tags" ;
@@ -27,18 +20,12 @@ export function compileCacheAssetsManifestSqlFile(options: BuildOptions) {
27
20
CREATE TABLE IF NOT EXISTS ${ JSON . stringify ( revalidationsTable ) } (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);\n`
28
21
) ;
29
22
30
- if ( existsSync ( rawManifestPath ) ) {
31
- const rawManifest : RawManifest = JSON . parse ( readFileSync ( rawManifestPath , "utf-8" ) ) ;
23
+ const values = metaFiles . map ( ( { tag, path } ) => `(${ JSON . stringify ( tag . S ) } , ${ JSON . stringify ( path . S ) } )` ) ;
32
24
33
- const values = rawManifest . map (
34
- ( { tag, path } ) => `(${ JSON . stringify ( tag . S ) } , ${ JSON . stringify ( path . S ) } )`
25
+ if ( values . length ) {
26
+ appendFileSync (
27
+ outputPath ,
28
+ `INSERT INTO ${ JSON . stringify ( tagsTable ) } (tag, path) VALUES ${ values . join ( ", " ) } ;`
35
29
) ;
36
-
37
- if ( values . length ) {
38
- appendFileSync (
39
- outputPath ,
40
- `INSERT INTO ${ JSON . stringify ( tagsTable ) } (tag, path) VALUES ${ values . join ( ", " ) } ;`
41
- ) ;
42
- }
43
30
}
44
31
}
0 commit comments