|
1 |
| -import fs from "node:fs"; |
2 |
| -import path from "node:path"; |
3 |
| - |
4 | 1 | import type { NextModeTagCache } from "types/overrides";
|
5 |
| -import { getMonorepoRelativePath } from "utils/normalize-path"; |
6 | 2 | import { debug } from "../../adapters/logger";
|
7 | 3 |
|
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>(); |
32 | 5 |
|
33 | 6 | export default {
|
34 | 7 | name: "fs-dev-nextMode",
|
35 | 8 | 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; |
40 | 25 | },
|
41 |
| - hasBeenRevalidated: async (tagsToCheck: string[], lastModified?: number) => { |
| 26 | + hasBeenRevalidated: async (tags: string[], lastModified?: number) => { |
42 | 27 | if (globalThis.openNextConfig.dangerous?.disableTagCache) {
|
43 | 28 | return false;
|
44 | 29 | }
|
45 |
| - debug("hasBeenRevalidated", { tags: tagsToCheck, lastModified }); |
46 | 30 |
|
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; |
57 | 34 | });
|
58 | 35 |
|
59 | 36 | debug("hasBeenRevalidated result:", hasRevalidatedTag);
|
60 | 37 | return hasRevalidatedTag;
|
61 | 38 | },
|
62 |
| - writeTags: async (tagsToWrite: string[]) => { |
| 39 | + writeTags: async (tags: string[]) => { |
63 | 40 | if (
|
64 | 41 | globalThis.openNextConfig.dangerous?.disableTagCache ||
|
65 |
| - tagsToWrite.length === 0 |
| 42 | + tags.length === 0 |
66 | 43 | ) {
|
67 | 44 | return;
|
68 | 45 | }
|
69 | 46 |
|
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 }); |
76 | 48 |
|
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 | + }); |
85 | 52 |
|
86 |
| - debug("writeTags completed, written", newTagObjects.length, "tags"); |
| 53 | + debug("writeTags completed, written", tags.length, "tags"); |
87 | 54 | },
|
88 | 55 | } satisfies NextModeTagCache;
|
0 commit comments