Skip to content

Commit 2a9d1db

Browse files
author
Nicolas Dorseuil
committed
add try catch around tag cache
1 parent c1ee8eb commit 2a9d1db

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import type { NextModeTagCache } from '@opennextjs/aws/types/overrides.js';
12
import doShardedTagCache from '@opennextjs/cloudflare/overrides/tag-cache/do-sharded-tag-cache';
23
import {
34
softTagFilter,
45
withFilter,
56
} from '@opennextjs/cloudflare/overrides/tag-cache/tag-cache-filter';
67

7-
export default withFilter({
8+
const filteredTagCache = withFilter({
89
tagCache: doShardedTagCache({
910
baseShardSize: 12,
1011
regionalCache: true,
@@ -16,3 +17,31 @@ export default withFilter({
1617
// We don't use `revalidatePath`, so we filter out soft tags
1718
filterFn: softTagFilter,
1819
});
20+
21+
export default {
22+
name: 'GitbookTagCache',
23+
mode: 'nextMode',
24+
getLastRevalidated: async (tags: string[]) => {
25+
try {
26+
return await filteredTagCache.getLastRevalidated(tags);
27+
} catch (error) {
28+
console.error('Error getting last revalidated tags:', error);
29+
return 0; // Fallback to 0 if there's an error
30+
}
31+
},
32+
hasBeenRevalidated: async (tags: string[]) => {
33+
try {
34+
return await filteredTagCache.hasBeenRevalidated(tags);
35+
} catch (error) {
36+
console.error('Error checking if tags have been revalidated:', error);
37+
return false; // Fallback to false if there's an error
38+
}
39+
},
40+
writeTags: async (tags: string[]) => {
41+
try {
42+
await filteredTagCache.writeTags(tags);
43+
} catch (error) {
44+
console.error('Error writing tags:', error);
45+
}
46+
},
47+
} satisfies NextModeTagCache;

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,29 @@ export default {
1212
name: 'GitbookTagCache',
1313
mode: 'nextMode',
1414
getLastRevalidated: async (tags: string[]) => {
15-
const { env } = getCloudflareContext();
16-
return (env as Env).MIDDLEWARE_REFERENCE?.getLastRevalidated(tags) ?? 0;
15+
try {
16+
const { env } = getCloudflareContext();
17+
return (env as Env).MIDDLEWARE_REFERENCE?.getLastRevalidated(tags) ?? 0;
18+
} catch (error) {
19+
console.error('Server - Error getting last revalidated tags:', error);
20+
return 0; // Fallback to 0 if there's an error
21+
}
1722
},
1823
hasBeenRevalidated: async (tags: string[]) => {
19-
const { env } = getCloudflareContext();
20-
return (env as Env).MIDDLEWARE_REFERENCE?.hasBeenRevalidated(tags) ?? false;
24+
try {
25+
const { env } = getCloudflareContext();
26+
return (env as Env).MIDDLEWARE_REFERENCE?.hasBeenRevalidated(tags) ?? false;
27+
} catch (error) {
28+
console.error('Server - Error checking if tags have been revalidated:', error);
29+
return false; // Fallback to false if there's an error
30+
}
2131
},
2232
writeTags: async (tags: string[]) => {
23-
const { env } = getCloudflareContext();
24-
(env as Env).MIDDLEWARE_REFERENCE?.writeTags(tags);
33+
try {
34+
const { env } = getCloudflareContext();
35+
(env as Env).MIDDLEWARE_REFERENCE?.writeTags(tags);
36+
} catch (error) {
37+
console.error('Server - Error writing tags:', error);
38+
}
2539
},
2640
} satisfies NextModeTagCache;

0 commit comments

Comments
 (0)