Skip to content

Commit b28b327

Browse files
committed
insert unique tags only, and use recoverableerror
1 parent e850841 commit b28b327

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { debug, error } from "@opennextjs/aws/adapters/logger.js";
22
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
33
import type { TagCache } from "@opennextjs/aws/types/overrides.js";
4+
import { RecoverableError } from "@opennextjs/aws/utils/error.js";
45

56
import { getCloudflareContext } from "./cloudflare-context.js";
67

@@ -36,7 +37,7 @@ class D1TagCache implements TagCache {
3637
.bind(path)
3738
.all<{ tag: string }>();
3839

39-
if (!success) throw new Error(`D1 select failed for ${path}`);
40+
if (!success) throw new RecoverableError(`D1 select failed for ${path}`);
4041

4142
const tags = results?.map((item) => this.removeBuildId(item.tag));
4243

@@ -60,7 +61,7 @@ class D1TagCache implements TagCache {
6061
.bind(tag)
6162
.all<{ path: string }>();
6263

63-
if (!success) throw new Error(`D1 select failed for ${tag}`);
64+
if (!success) throw new RecoverableError(`D1 select failed for ${tag}`);
6465

6566
const paths = results?.map((item) => this.removeBuildId(item.path));
6667

@@ -86,7 +87,7 @@ class D1TagCache implements TagCache {
8687
.bind(this.getCacheKey(path), lastModified ?? 0)
8788
.all<{ tag: string }>();
8889

89-
if (!success) throw new Error(`D1 select failed for ${path} - ${lastModified ?? 0}`);
90+
if (!success) throw new RecoverableError(`D1 select failed for ${path} - ${lastModified ?? 0}`);
9091

9192
debug("revalidatedTags", results);
9293
return results?.length > 0 ? -1 : (lastModified ?? Date.now());
@@ -101,26 +102,36 @@ class D1TagCache implements TagCache {
101102
if (isDisabled || tags.length === 0) return;
102103

103104
try {
105+
const uniqueTags = new Set<string>();
104106
const results = await db.batch(
105-
tags.map(({ tag, path, revalidatedAt }) => {
107+
tags.reduce<D1PreparedStatement[]>((acc, { tag, path, revalidatedAt }) => {
106108
if (revalidatedAt === 1) {
107109
// new tag/path mapping from set
108-
return db
109-
.prepare(`INSERT INTO ${table.tags} (tag, path) VALUES (?, ?)`)
110-
.bind(this.getCacheKey(tag), this.getCacheKey(path));
110+
acc.push(
111+
db
112+
.prepare(`INSERT INTO ${table.tags} (tag, path) VALUES (?, ?)`)
113+
.bind(this.getCacheKey(tag), this.getCacheKey(path))
114+
);
111115
}
112116

113-
// tag was revalidated
114-
return db
115-
.prepare(`INSERT INTO ${table.revalidations} (tag, revalidatedAt) VALUES (?, ?)`)
116-
.bind(this.getCacheKey(tag), revalidatedAt ?? Date.now());
117-
})
117+
if (!uniqueTags.has(tag) && revalidatedAt !== -1) {
118+
// tag was revalidated
119+
uniqueTags.add(tag);
120+
acc.push(
121+
db
122+
.prepare(`INSERT INTO ${table.revalidations} (tag, revalidatedAt) VALUES (?, ?)`)
123+
.bind(this.getCacheKey(tag), revalidatedAt ?? Date.now())
124+
);
125+
}
126+
127+
return acc;
128+
}, [])
118129
);
119130

120131
const failedResults = results.filter((res) => !res.success);
121132

122133
if (failedResults.length > 0) {
123-
throw new Error(`${failedResults.length} tags failed to write`);
134+
throw new RecoverableError(`${failedResults.length} tags failed to write`);
124135
}
125136
} catch (e) {
126137
error("Failed to batch write tags", e);

0 commit comments

Comments
 (0)