Skip to content

Commit 1168427

Browse files
author
Nicolas Dorseuil
committed
fix tag cache
1 parent e392a44 commit 1168427

File tree

2 files changed

+6
-60
lines changed

2 files changed

+6
-60
lines changed

packages/gitbook-v2/openNext/customWorkers/do.js

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,10 @@ export class R2WriteBuffer extends DurableObject {
1919
}
2020
}
2121

22-
// TODO: replace once i figured out why `revalidateTag` is not working in preview
23-
export class DOShardedTagCache extends DurableObject {
24-
sql;
25-
26-
constructor(state, env) {
27-
super(state, env);
28-
this.sql = state.storage.sql;
29-
state.blockConcurrencyWhile(async () => {
30-
this.sql.exec(
31-
'CREATE TABLE IF NOT EXISTS revalidations (tag TEXT PRIMARY KEY, revalidatedAt INTEGER)'
32-
);
33-
});
34-
}
35-
36-
async getLastRevalidated(tags) {
37-
try {
38-
const result = this.sql
39-
.exec(
40-
`SELECT MAX(revalidatedAt) AS time FROM revalidations WHERE tag IN (${tags.map(() => '?').join(', ')})`,
41-
...tags
42-
)
43-
.toArray();
44-
console.log('getLastRevalidated', tags, result);
45-
if (result.length === 0) return 0;
46-
// We only care about the most recent revalidation
47-
return result[0]?.time ?? 0;
48-
} catch (e) {
49-
console.error(e);
50-
// By default we don't want to crash here, so we return 0
51-
return 0;
52-
}
53-
}
54-
55-
async hasBeenRevalidated(tags, lastModified) {
56-
const result = this.sql
57-
.exec(
58-
`SELECT 1 FROM revalidations WHERE tag IN (${tags.map(() => '?').join(', ')}) AND revalidatedAt > ? LIMIT 1`,
59-
...tags,
60-
lastModified ?? Date.now()
61-
)
62-
.toArray();
63-
64-
console.log('hasBeenRevalidated', tags, lastModified, result);
65-
return result.length > 0;
66-
}
67-
68-
async writeTags(tags, lastModified) {
69-
console.log('writeTags', tags, lastModified);
70-
tags.forEach((tag) => {
71-
this.sql.exec(
72-
'INSERT OR REPLACE INTO revalidations (tag, revalidatedAt) VALUES (?, ?)',
73-
tag,
74-
lastModified
75-
);
76-
});
77-
}
78-
}
79-
8022
export { DOQueueHandler } from '../../.open-next/.build/durable-objects/queue.js';
8123

24+
export { DOShardedTagCache } from '../../.open-next/.build/durable-objects/sharded-tag-cache.js';
25+
8226
export default {
8327
async fetch() {
8428
// This worker does not handle any requests, it only provides Durable Objects

packages/gitbook-v2/openNext/tagCache/middleware.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { softTagFilter } from '@opennextjs/cloudflare/overrides/tag-cache/tag-ca
66
const originalTagCache = doShardedTagCache({
77
baseShardSize: 12,
88
regionalCache: true,
9+
// We can probably increase this value even further
10+
regionalCacheTtlSec: 60,
911
shardReplication: {
1012
numberOfSoftReplicas: 2,
1113
numberOfHardReplicas: 1,
@@ -35,7 +37,7 @@ export default {
3537
}
3638
);
3739
},
38-
hasBeenRevalidated: async (tags: string[]) => {
40+
hasBeenRevalidated: async (tags: string[], lastModified?: number) => {
3941
const tagsToCheck = tags.filter(softTagFilter);
4042
if (tagsToCheck.length === 0) {
4143
// If we reach here, it probably means that there is an issue that we'll need to address.
@@ -51,7 +53,7 @@ export default {
5153
name: tagsToCheck.join(', '),
5254
},
5355
async () => {
54-
const result = await originalTagCache.hasBeenRevalidated(tagsToCheck);
56+
const result = await originalTagCache.hasBeenRevalidated(tagsToCheck, lastModified);
5557
return result;
5658
}
5759
);

0 commit comments

Comments
 (0)