|
| 1 | +import type { NextModeTagCache } from "types/overrides"; |
| 2 | +import { debug } from "../../adapters/logger"; |
| 3 | + |
| 4 | +const tagsMap = new Map<string, number>(); |
| 5 | + |
| 6 | +export default { |
| 7 | + name: "fs-dev-nextMode", |
| 8 | + mode: "nextMode", |
| 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; |
| 25 | + }, |
| 26 | + hasBeenRevalidated: async (tags: string[], lastModified?: number) => { |
| 27 | + if (globalThis.openNextConfig.dangerous?.disableTagCache) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + const hasRevalidatedTag = tags.some((tag) => { |
| 32 | + const tagRevalidatedAt = tagsMap.get(tag); |
| 33 | + return tagRevalidatedAt ? tagRevalidatedAt > (lastModified ?? 0) : false; |
| 34 | + }); |
| 35 | + |
| 36 | + debug("hasBeenRevalidated result:", hasRevalidatedTag); |
| 37 | + return hasRevalidatedTag; |
| 38 | + }, |
| 39 | + writeTags: async (tags: string[]) => { |
| 40 | + if ( |
| 41 | + globalThis.openNextConfig.dangerous?.disableTagCache || |
| 42 | + tags.length === 0 |
| 43 | + ) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + debug("writeTags", { tags: tags }); |
| 48 | + |
| 49 | + tags.forEach((tag) => { |
| 50 | + tagsMap.set(tag, Date.now()); |
| 51 | + }); |
| 52 | + |
| 53 | + debug("writeTags completed, written", tags.length, "tags"); |
| 54 | + }, |
| 55 | +} satisfies NextModeTagCache; |
0 commit comments