@@ -4,37 +4,34 @@ import type { OriginalTagCache } from "@opennextjs/aws/types/overrides.js";
44import { RecoverableError } from "@opennextjs/aws/utils/error.js" ;
55
66import { getCloudflareContext } from "../../cloudflare-context.js" ;
7- import { DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE , DEFAULT_NEXT_CACHE_D1_TAGS_TABLE } from "./internal.js" ;
87
98/**
109 * An instance of the Tag Cache that uses a D1 binding (`NEXT_CACHE_D1`) as it's underlying data store.
1110 *
1211 * **Tag/path mappings table**
1312 *
1413 * Information about the relation between tags and paths is stored in a `tags` table that contains
15- * two columns; `tag`, and `path`. The table name can be configured with `NEXT_CACHE_D1_TAGS_TABLE`
16- * environment variable.
14+ * two columns; `tag`, and `path`.
1715 *
1816 * This table should be populated using an SQL file that is generated during the build process.
1917 *
2018 * **Tag revalidations table**
2119 *
2220 * Revalidation times for tags are stored in a `revalidations` table that contains two columns; `tags`,
23- * and `revalidatedAt`. The table name can be configured with `NEXT_CACHE_D1_REVALIDATIONS_TABLE`
24- * environment variable.
21+ * and `revalidatedAt`..
2522 */
2623class D1TagCache implements OriginalTagCache {
2724 public readonly name = "d1-tag-cache" ;
2825
2926 public async getByPath ( rawPath : string ) : Promise < string [ ] > {
30- const { isDisabled, db, tables } = this . getConfig ( ) ;
27+ const { isDisabled, db } = this . getConfig ( ) ;
3128 if ( isDisabled ) return [ ] ;
3229
3330 const path = this . getCacheKey ( rawPath ) ;
3431
3532 try {
3633 const { success, results } = await db
37- . prepare ( `SELECT tag FROM ${ JSON . stringify ( tables . tags ) } WHERE path = ?` )
34+ . prepare ( `SELECT tag FROM tags WHERE path = ?` )
3835 . bind ( path )
3936 . all < { tag : string } > ( ) ;
4037
@@ -51,14 +48,14 @@ class D1TagCache implements OriginalTagCache {
5148 }
5249
5350 public async getByTag ( rawTag : string ) : Promise < string [ ] > {
54- const { isDisabled, db, tables } = this . getConfig ( ) ;
51+ const { isDisabled, db } = this . getConfig ( ) ;
5552 if ( isDisabled ) return [ ] ;
5653
5754 const tag = this . getCacheKey ( rawTag ) ;
5855
5956 try {
6057 const { success, results } = await db
61- . prepare ( `SELECT path FROM ${ JSON . stringify ( tables . tags ) } WHERE tag = ?` )
58+ . prepare ( `SELECT path FROM tags WHERE tag = ?` )
6259 . bind ( tag )
6360 . all < { path : string } > ( ) ;
6461
@@ -75,15 +72,15 @@ class D1TagCache implements OriginalTagCache {
7572 }
7673
7774 public async getLastModified ( path : string , lastModified ?: number ) : Promise < number > {
78- const { isDisabled, db, tables } = this . getConfig ( ) ;
75+ const { isDisabled, db } = this . getConfig ( ) ;
7976 if ( isDisabled ) return lastModified ?? Date . now ( ) ;
8077
8178 try {
8279 const { success, results } = await db
8380 . prepare (
84- `SELECT ${ JSON . stringify ( tables . revalidations ) } .tag FROM ${ JSON . stringify ( tables . revalidations ) }
85- INNER JOIN ${ JSON . stringify ( tables . tags ) } ON ${ JSON . stringify ( tables . revalidations ) } .tag = ${ JSON . stringify ( tables . tags ) } .tag
86- WHERE ${ JSON . stringify ( tables . tags ) } .path = ? AND ${ JSON . stringify ( tables . revalidations ) } .revalidatedAt > ?;`
81+ `SELECT revalidations.tag FROM revalidations
82+ INNER JOIN tags ON revalidations.tag = tags.tag
83+ WHERE tags.path = ? AND revalidations.revalidatedAt > ?;`
8784 )
8885 . bind ( this . getCacheKey ( path ) , lastModified ?? 0 )
8986 . all < { tag : string } > ( ) ;
@@ -99,7 +96,7 @@ class D1TagCache implements OriginalTagCache {
9996 }
10097
10198 public async writeTags ( tags : { tag : string ; path : string ; revalidatedAt ?: number } [ ] ) : Promise < void > {
102- const { isDisabled, db, tables } = this . getConfig ( ) ;
99+ const { isDisabled, db } = this . getConfig ( ) ;
103100 if ( isDisabled || tags . length === 0 ) return ;
104101
105102 try {
@@ -110,17 +107,15 @@ class D1TagCache implements OriginalTagCache {
110107 if ( revalidatedAt === 1 ) {
111108 // new tag/path mapping from set
112109 return db
113- . prepare ( `INSERT INTO ${ JSON . stringify ( tables . tags ) } (tag, path) VALUES (?, ?)` )
110+ . prepare ( `INSERT INTO tags (tag, path) VALUES (?, ?)` )
114111 . bind ( this . getCacheKey ( tag ) , this . getCacheKey ( path ) ) ;
115112 }
116113
117114 if ( ! uniqueTags . has ( tag ) && revalidatedAt !== - 1 ) {
118115 // tag was revalidated
119116 uniqueTags . add ( tag ) ;
120117 return db
121- . prepare (
122- `INSERT INTO ${ JSON . stringify ( tables . revalidations ) } (tag, revalidatedAt) VALUES (?, ?)`
123- )
118+ . prepare ( `INSERT INTO revalidations (tag, revalidatedAt) VALUES (?, ?)` )
124119 . bind ( this . getCacheKey ( tag ) , revalidatedAt ?? Date . now ( ) ) ;
125120 }
126121 } )
@@ -146,18 +141,12 @@ class D1TagCache implements OriginalTagCache {
146141 const isDisabled = ! ! ( globalThis as unknown as { openNextConfig : OpenNextConfig } ) . openNextConfig
147142 . dangerous ?. disableTagCache ;
148143
149- if ( ! db || isDisabled ) {
150- return { isDisabled : true as const } ;
151- }
152-
153- return {
154- isDisabled : false as const ,
155- db,
156- tables : {
157- tags : cfEnv . NEXT_CACHE_D1_TAGS_TABLE ?? DEFAULT_NEXT_CACHE_D1_TAGS_TABLE ,
158- revalidations : cfEnv . NEXT_CACHE_D1_REVALIDATIONS_TABLE ?? DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE ,
159- } ,
160- } ;
144+ return ! db || isDisabled
145+ ? { isDisabled : true as const }
146+ : {
147+ isDisabled : false as const ,
148+ db,
149+ } ;
161150 }
162151
163152 protected removeBuildId ( key : string ) {
0 commit comments