Skip to content

Commit f9751a2

Browse files
committed
fixup! drop D1 custom table names
1 parent d948e2e commit f9751a2

File tree

7 files changed

+40
-74
lines changed

7 files changed

+40
-74
lines changed

packages/cloudflare/src/api/cloudflare-context.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ declare global {
2525

2626
// D1 db used for the tag cache
2727
NEXT_CACHE_D1?: D1Database;
28-
// D1 table to use for the tag cache for the tag/path mapping
29-
// Optional, default to "tags"
30-
NEXT_CACHE_D1_TAGS_TABLE?: string;
31-
// D1 table to use for the tag cache for storing the tag and their associated revalidation times
32-
// Optional, default to "revalidations"
33-
NEXT_CACHE_D1_REVALIDATIONS_TABLE?: string;
3428

3529
// Durable Object namespace to use for the durable object queue handler
3630
NEXT_CACHE_DO_REVALIDATION?: DurableObjectNamespace<DurableObjectQueueHandler>;

packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";
44
import { RecoverableError } from "@opennextjs/aws/utils/error.js";
55

66
import { getCloudflareContext } from "../../cloudflare-context.js";
7-
import { DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE } from "./internal.js";
87

98
export class D1NextModeTagCache implements NextModeTagCache {
109
readonly mode = "nextMode" as const;
1110
readonly name = "d1-next-mode-tag-cache";
1211

1312
async hasBeenRevalidated(tags: string[], lastModified?: number): Promise<boolean> {
14-
const { isDisabled, db, tables } = this.getConfig();
13+
const { isDisabled, db } = this.getConfig();
1514
if (isDisabled) return false;
1615
try {
1716
const result = await db
1817
.prepare(
19-
`SELECT COUNT(*) as cnt FROM ${JSON.stringify(tables.revalidations)} WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ? LIMIT 1`
18+
`SELECT COUNT(*) as cnt FROM revalidations WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ? LIMIT 1`
2019
)
2120
.bind(...tags.map((tag) => this.getCacheKey(tag)), lastModified ?? Date.now())
2221
.first<{ cnt: number }>();
@@ -32,12 +31,12 @@ export class D1NextModeTagCache implements NextModeTagCache {
3231
}
3332

3433
async writeTags(tags: string[]): Promise<void> {
35-
const { isDisabled, db, tables } = this.getConfig();
34+
const { isDisabled, db } = this.getConfig();
3635
if (isDisabled) return Promise.resolve();
3736
const result = await db.batch(
3837
tags.map((tag) =>
3938
db
40-
.prepare(`INSERT INTO ${JSON.stringify(tables.revalidations)} (tag, revalidatedAt) VALUES (?, ?)`)
39+
.prepare(`INSERT INTO revalidations (tag, revalidatedAt) VALUES (?, ?)`)
4140
.bind(this.getCacheKey(tag), Date.now())
4241
)
4342
);
@@ -53,17 +52,12 @@ export class D1NextModeTagCache implements NextModeTagCache {
5352
const isDisabled = !!(globalThis as unknown as { openNextConfig: OpenNextConfig }).openNextConfig
5453
.dangerous?.disableTagCache;
5554

56-
if (!db || isDisabled) {
57-
return { isDisabled: true as const };
58-
}
59-
60-
return {
61-
isDisabled: false as const,
62-
db,
63-
tables: {
64-
revalidations: cfEnv.NEXT_CACHE_D1_REVALIDATIONS_TABLE ?? DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE,
65-
},
66-
};
55+
return !db || isDisabled
56+
? { isDisabled: true as const }
57+
: {
58+
isDisabled: false as const,
59+
db,
60+
};
6761
}
6862

6963
protected removeBuildId(key: string) {

packages/cloudflare/src/api/overrides/tag-cache/d1-tag-cache.ts

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,34 @@ import type { OriginalTagCache } from "@opennextjs/aws/types/overrides.js";
44
import { RecoverableError } from "@opennextjs/aws/utils/error.js";
55

66
import { 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
*/
2623
class 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) {

packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,12 @@ class ShardedDOTagCache implements NextModeTagCache {
195195
const isDisabled = !!(globalThis as unknown as { openNextConfig: OpenNextConfig }).openNextConfig
196196
.dangerous?.disableTagCache;
197197

198-
if (!db || isDisabled) {
199-
return { isDisabled: true as const };
200-
}
201-
202-
return {
203-
isDisabled: false as const,
204-
db,
205-
};
198+
return !db || isDisabled
199+
? { isDisabled: true as const }
200+
: {
201+
isDisabled: false as const,
202+
db,
203+
};
206204
}
207205

208206
/**

packages/cloudflare/src/api/overrides/tag-cache/internal.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/cloudflare/src/cli/build/open-next/compile-cache-assets-manifest.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,21 @@ import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
55
import type { TagCacheMetaFile } from "@opennextjs/aws/types/cache.js";
66

77
/**
8-
* Generates SQL statements that can be used to initialise the cache assets manifest in an SQL data store.
8+
* Generates SQL statements that can be used to initialize the cache assets manifest in an SQL data store.
99
*/
1010
export function compileCacheAssetsManifestSqlFile(options: BuildOptions, metaFiles: TagCacheMetaFile[]) {
1111
const outputPath = path.join(options.outputDir, "cloudflare/cache-assets-manifest.sql");
1212

13-
const tagsTable = process.env.NEXT_CACHE_D1_TAGS_TABLE || "tags";
14-
const revalidationsTable = process.env.NEXT_CACHE_D1_REVALIDATIONS_TABLE || "revalidations";
15-
1613
mkdirSync(path.dirname(outputPath), { recursive: true });
1714
writeFileSync(
1815
outputPath,
19-
`CREATE TABLE IF NOT EXISTS ${JSON.stringify(tagsTable)} (tag TEXT NOT NULL, path TEXT NOT NULL, UNIQUE(tag, path) ON CONFLICT REPLACE);
20-
CREATE TABLE IF NOT EXISTS ${JSON.stringify(revalidationsTable)} (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);\n`
16+
`CREATE TABLE IF NOT EXISTS tags (tag TEXT NOT NULL, path TEXT NOT NULL, UNIQUE(tag, path) ON CONFLICT REPLACE);
17+
CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);\n`
2118
);
2219

2320
const values = metaFiles.map(({ tag, path }) => `(${JSON.stringify(tag.S)}, ${JSON.stringify(path.S)})`);
2421

2522
if (values.length) {
26-
appendFileSync(
27-
outputPath,
28-
`INSERT INTO ${JSON.stringify(tagsTable)} (tag, path) VALUES ${values.join(", ")};`
29-
);
23+
appendFileSync(outputPath, `INSERT INTO tags (tag, path) VALUES ${values.join(", ")};`);
3024
}
3125
}

packages/cloudflare/src/cli/populate-cache/populate-cache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,13 @@ export async function populateCache(
9090
switch (name) {
9191
case "d1-next-mode-tag-cache": {
9292
logger.info("\nCreating D1 table if necessary...");
93-
const revalidationsTable = process.env.NEXT_CACHE_D1_REVALIDATIONS_TABLE || "revalidations";
9493

9594
runWrangler(
9695
options,
9796
[
9897
"d1 execute",
9998
"NEXT_CACHE_D1",
100-
`--command "CREATE TABLE IF NOT EXISTS ${JSON.stringify(revalidationsTable)} (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"`,
99+
`--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"`,
101100
],
102101
{ ...populateCacheOptions, logging: "error" }
103102
);

0 commit comments

Comments
 (0)