Skip to content

Commit b355d74

Browse files
committed
make it work with original mode
1 parent 84142d2 commit b355d74

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

packages/open-next/src/adapters/composable-cache.ts

+53-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ export default {
1515

1616
debug("composable cache result", result);
1717

18+
// We need to check if the tags associated with this entry has been revalidated
19+
if (
20+
globalThis.tagCache.mode === "nextMode" &&
21+
result.value.tags.length > 0
22+
) {
23+
const hasBeenRevalidated = await globalThis.tagCache.hasBeenRevalidated(
24+
result.value.tags,
25+
result.lastModified,
26+
);
27+
if (hasBeenRevalidated) return undefined;
28+
} else if (
29+
globalThis.tagCache.mode === "original" ||
30+
globalThis.tagCache.mode === undefined
31+
) {
32+
const hasBeenRevalidated =
33+
(await globalThis.tagCache.getLastModified(
34+
cacheKey,
35+
result.lastModified,
36+
)) === -1;
37+
if (hasBeenRevalidated) return undefined;
38+
}
39+
1840
return {
1941
...result.value,
2042
value: toReadableStream(result.value.value),
@@ -36,6 +58,15 @@ export default {
3658
},
3759
"composable",
3860
);
61+
if (globalThis.tagCache.mode === "original") {
62+
const storedTags = await globalThis.tagCache.getByPath(cacheKey);
63+
const tagsToWrite = entry.tags.filter((tag) => !storedTags.includes(tag));
64+
if (tagsToWrite.length > 0) {
65+
await globalThis.tagCache.writeTags(
66+
tagsToWrite.map((tag) => ({ tag, path: cacheKey })),
67+
);
68+
}
69+
}
3970
},
4071

4172
async refreshTags() {
@@ -46,12 +77,33 @@ export default {
4677
if (globalThis.tagCache.mode === "nextMode") {
4778
return globalThis.tagCache.getLastRevalidated(tags);
4879
}
49-
//TODO: Not supported for now - I'll need to figure out a way, maybe we'd want to merge both type into one
80+
// We always return 0 here, original tag cache are handled directly in the get part
81+
// TODO: We need to test this more, i'm not entirely sure that this is working as expected
5082
return 0;
5183
},
5284
async expireTags(...tags: string[]) {
5385
if (globalThis.tagCache.mode === "nextMode") {
5486
return globalThis.tagCache.writeTags(tags);
5587
}
88+
const tagCache = globalThis.tagCache;
89+
const revalidatedAt = Date.now();
90+
// For the original mode, we have more work to do here.
91+
// We need to find all paths linked to to these tags
92+
const pathsToUpdate = await Promise.all(
93+
tags.map(async (tag) => {
94+
const paths = await tagCache.getByTag(tag);
95+
return paths.map((path) => ({
96+
path,
97+
tag,
98+
revalidatedAt,
99+
}));
100+
}),
101+
);
102+
// We need to deduplicate paths, we use a set for that
103+
const setToWrite = new Set<{ path: string; tag: string }>();
104+
for (const entry of pathsToUpdate.flat()) {
105+
setToWrite.add(entry);
106+
}
107+
await globalThis.tagCache.writeTags(Array.from(setToWrite));
56108
},
57109
} satisfies ComposableCacheHandler;

packages/open-next/src/overrides/tagCache/fs-dev.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { TagCache } from "types/overrides";
22

33
import fs from "node:fs";
44

5+
// TODO: fix this for monorepo
56
const tagFile = "../../dynamodb-provider/dynamodb-cache.json";
67

78
const tagContent = fs.readFileSync(tagFile, "utf-8");
@@ -44,7 +45,7 @@ const tagCache: TagCache = {
4445
newTags.map((tag) => ({
4546
tag: { S: tag.tag },
4647
path: { S: tag.path },
47-
revalidatedAt: { N: String(tag.revalidatedAt) },
48+
revalidatedAt: { N: String(tag.revalidatedAt ?? 1) },
4849
})),
4950
);
5051
},

0 commit comments

Comments
 (0)