@@ -13,6 +13,8 @@ import { getCloudflareContext } from "./cloudflare-context.js";
1313 * two columns; `tag`, and `path`. The table name can be configured with `NEXT_CACHE_D1_TAGS_TABLE`
1414 * environment variable.
1515 *
16+ * This table should be populated using an SQL file that is generated during the build process.
17+ *
1618 * **Tag revalidations**
1719 *
1820 * Revalidation times for tags are stored in a `revalidations` table that contains two columns; `tags`,
@@ -30,7 +32,7 @@ class D1TagCache implements TagCache {
3032
3133 try {
3234 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 = ?` )
3436 . bind ( path )
3537 . all < { tag : string } > ( ) ;
3638
@@ -54,7 +56,7 @@ class D1TagCache implements TagCache {
5456
5557 try {
5658 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 = ?` )
5860 . bind ( tag )
5961 . all < { path : string } > ( ) ;
6062
@@ -77,9 +79,9 @@ class D1TagCache implements TagCache {
7779 try {
7880 const { success, results } = await db
7981 . 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 > ?;`
8385 )
8486 . bind ( this . getCacheKey ( path ) , lastModified ?? 0 )
8587 . all < { tag : string } > ( ) ;
@@ -104,13 +106,13 @@ class D1TagCache implements TagCache {
104106 if ( revalidatedAt === 1 ) {
105107 // new tag/path mapping from set
106108 return db
107- . prepare ( `INSERT INTO ${ JSON . stringify ( table . tags ) } (tag, path) VALUES (?, ?)` )
109+ . prepare ( `INSERT INTO ${ table . tags } (tag, path) VALUES (?, ?)` )
108110 . bind ( this . getCacheKey ( tag ) , this . getCacheKey ( path ) ) ;
109111 }
110112
111113 // tag was revalidated
112114 return db
113- . prepare ( `INSERT INTO ${ JSON . stringify ( table . revalidations ) } (tag, revalidatedAt) VALUES (?, ?)` )
115+ . prepare ( `INSERT INTO ${ table . revalidations } (tag, revalidatedAt) VALUES (?, ?)` )
114116 . bind ( this . getCacheKey ( tag ) , revalidatedAt ?? Date . now ( ) ) ;
115117 } )
116118 ) ;
@@ -142,8 +144,8 @@ class D1TagCache implements TagCache {
142144 isDisabled : false as const ,
143145 db,
144146 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" ) ,
147149 } ,
148150 } ;
149151 }
0 commit comments