@@ -13,6 +13,8 @@ import { getCloudflareContext } from "./cloudflare-context.js";
13
13
* two columns; `tag`, and `path`. The table name can be configured with `NEXT_CACHE_D1_TAGS_TABLE`
14
14
* environment variable.
15
15
*
16
+ * This table should be populated using an SQL file that is generated during the build process.
17
+ *
16
18
* **Tag revalidations**
17
19
*
18
20
* Revalidation times for tags are stored in a `revalidations` table that contains two columns; `tags`,
@@ -30,7 +32,7 @@ class D1TagCache implements TagCache {
30
32
31
33
try {
32
34
const { success, results } = await db
33
- . prepare ( `SELECT tag FROM ${ JSON . stringify ( table . tags ) } WHERE path = ?` )
35
+ . prepare ( `SELECT tag FROM ${ table . tags } WHERE path = ?` )
34
36
. bind ( path )
35
37
. all < { tag : string } > ( ) ;
36
38
@@ -54,7 +56,7 @@ class D1TagCache implements TagCache {
54
56
55
57
try {
56
58
const { success, results } = await db
57
- . prepare ( `SELECT path FROM ${ JSON . stringify ( table . tags ) } WHERE tag = ?` )
59
+ . prepare ( `SELECT path FROM ${ table . tags } WHERE tag = ?` )
58
60
. bind ( tag )
59
61
. all < { path : string } > ( ) ;
60
62
@@ -77,9 +79,9 @@ class D1TagCache implements TagCache {
77
79
try {
78
80
const { success, results } = await db
79
81
. prepare (
80
- `SELECT ${ JSON . stringify ( table . revalidations ) } .tag FROM ${ JSON . stringify ( table . revalidations ) }
81
- INNER JOIN ${ JSON . stringify ( table . tags ) } ON ${ JSON . stringify ( table . revalidations ) } .tag = ${ JSON . stringify ( table . tags ) } .tag
82
- WHERE ${ JSON . stringify ( table . tags ) } .path = ? AND ${ JSON . stringify ( table . revalidations ) } .revalidatedAt > ?;`
82
+ `SELECT ${ table . revalidations } .tag FROM ${ table . revalidations }
83
+ INNER JOIN ${ table . tags } ON ${ table . revalidations } .tag = ${ table . tags } .tag
84
+ WHERE ${ table . tags } .path = ? AND ${ table . revalidations } .revalidatedAt > ?;`
83
85
)
84
86
. bind ( this . getCacheKey ( path ) , lastModified ?? 0 )
85
87
. all < { tag : string } > ( ) ;
@@ -104,13 +106,13 @@ class D1TagCache implements TagCache {
104
106
if ( revalidatedAt === 1 ) {
105
107
// new tag/path mapping from set
106
108
return db
107
- . prepare ( `INSERT INTO ${ JSON . stringify ( table . tags ) } (tag, path) VALUES (?, ?)` )
109
+ . prepare ( `INSERT INTO ${ table . tags } (tag, path) VALUES (?, ?)` )
108
110
. bind ( this . getCacheKey ( tag ) , this . getCacheKey ( path ) ) ;
109
111
}
110
112
111
113
// tag was revalidated
112
114
return db
113
- . prepare ( `INSERT INTO ${ JSON . stringify ( table . revalidations ) } (tag, revalidatedAt) VALUES (?, ?)` )
115
+ . prepare ( `INSERT INTO ${ table . revalidations } (tag, revalidatedAt) VALUES (?, ?)` )
114
116
. bind ( this . getCacheKey ( tag ) , revalidatedAt ?? Date . now ( ) ) ;
115
117
} )
116
118
) ;
@@ -142,8 +144,8 @@ class D1TagCache implements TagCache {
142
144
isDisabled : false as const ,
143
145
db,
144
146
table : {
145
- tags : cfEnv . NEXT_CACHE_D1_TAGS_TABLE ?? "tags" ,
146
- revalidations : cfEnv . NEXT_CACHE_D1_REVALIDATIONS_TABLE ?? "revalidations" ,
147
+ tags : JSON . stringify ( cfEnv . NEXT_CACHE_D1_TAGS_TABLE ?? "tags" ) ,
148
+ revalidations : JSON . stringify ( cfEnv . NEXT_CACHE_D1_REVALIDATIONS_TABLE ?? "revalidations" ) ,
147
149
} ,
148
150
} ;
149
151
}
0 commit comments