Skip to content

Commit c7e4796

Browse files
committed
review
1 parent 71603ae commit c7e4796

File tree

1 file changed

+28
-61
lines changed

1 file changed

+28
-61
lines changed
Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,55 @@
1-
import fs from "node:fs";
2-
import path from "node:path";
3-
41
import type { NextModeTagCache } from "types/overrides";
5-
import { getMonorepoRelativePath } from "utils/normalize-path";
62
import { debug } from "../../adapters/logger";
73

8-
const tagFile = path.join(
9-
getMonorepoRelativePath(),
10-
"dynamodb-provider/dynamodb-cache.json",
11-
);
12-
const tagContent = fs.readFileSync(tagFile, "utf-8");
13-
14-
let tags = JSON.parse(tagContent) as {
15-
tag: { S: string };
16-
path: { S: string };
17-
revalidatedAt: { N: string };
18-
}[];
19-
20-
function buildKey(key: string) {
21-
const { NEXT_BUILD_ID } = process.env;
22-
return path.posix.join(NEXT_BUILD_ID ?? "", key);
23-
}
24-
25-
function buildObject(tag: string, revalidatedAt?: number) {
26-
return {
27-
path: { S: buildKey(tag) },
28-
tag: { S: buildKey(tag) },
29-
revalidatedAt: { N: `${revalidatedAt ?? Date.now()}` },
30-
};
31-
}
4+
const tagsMap = new Map<string, number>();
325

336
export default {
347
name: "fs-dev-nextMode",
358
mode: "nextMode",
36-
getLastRevalidated: async (tagsToCheck: string[]) => {
37-
// Not supported for now
38-
// TODO: Implement getLastRevalidated
39-
return 0;
9+
getLastRevalidated: async (tags: string[]) => {
10+
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
11+
return 0;
12+
}
13+
14+
let lastRevalidated = 0;
15+
16+
tags.forEach((tag) => {
17+
const tagTime = tagsMap.get(tag);
18+
if (tagTime && tagTime > lastRevalidated) {
19+
lastRevalidated = tagTime;
20+
}
21+
});
22+
23+
debug("getLastRevalidated result:", lastRevalidated);
24+
return lastRevalidated;
4025
},
41-
hasBeenRevalidated: async (tagsToCheck: string[], lastModified?: number) => {
26+
hasBeenRevalidated: async (tags: string[], lastModified?: number) => {
4227
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
4328
return false;
4429
}
45-
debug("hasBeenRevalidated", { tags: tagsToCheck, lastModified });
4630

47-
// Build the cache keys for the tags we're checking
48-
const cacheKeys = tagsToCheck.map((tag) => buildKey(tag));
49-
50-
// Check if any tag has been revalidated after the lastModified time
51-
const hasRevalidatedTag = tags.some((tagEntry) => {
52-
const tagRevalidatedAt = Number.parseInt(tagEntry.revalidatedAt.N);
53-
return (
54-
cacheKeys.includes(tagEntry.tag.S) &&
55-
tagRevalidatedAt > (lastModified ?? Date.now())
56-
);
31+
const hasRevalidatedTag = tags.some((tag) => {
32+
const lastModifiedTime = tagsMap.get(tag);
33+
return lastModifiedTime ? lastModifiedTime > (lastModified ?? 0) : false;
5734
});
5835

5936
debug("hasBeenRevalidated result:", hasRevalidatedTag);
6037
return hasRevalidatedTag;
6138
},
62-
writeTags: async (tagsToWrite: string[]) => {
39+
writeTags: async (tags: string[]) => {
6340
if (
6441
globalThis.openNextConfig.dangerous?.disableTagCache ||
65-
tagsToWrite.length === 0
42+
tags.length === 0
6643
) {
6744
return;
6845
}
6946

70-
debug("writeTags", { tags: tagsToWrite });
71-
72-
// Create new tag objects to write
73-
const newTagObjects = tagsToWrite.map((tag) =>
74-
buildObject(tag, Date.now()),
75-
);
47+
debug("writeTags", { tags: tags });
7648

77-
// Remove any existing entries for these tags to avoid duplicates
78-
const existingTagKeys = newTagObjects.map((obj) => obj.tag.S);
79-
tags = tags.filter((tagEntry) => !existingTagKeys.includes(tagEntry.tag.S));
80-
81-
// Add the new tags
82-
tags.push(...newTagObjects);
83-
84-
fs.writeFileSync(tagFile, JSON.stringify(tags));
49+
tags.forEach((tag) => {
50+
tagsMap.set(tag, Date.now());
51+
});
8552

86-
debug("writeTags completed, written", newTagObjects.length, "tags");
53+
debug("writeTags completed, written", tags.length, "tags");
8754
},
8855
} satisfies NextModeTagCache;

0 commit comments

Comments
 (0)