|
| 1 | +import { debug } from "@opennextjs/aws/adapters/logger.js"; |
| 2 | +import { generateMessageGroupId } from "@opennextjs/aws/core/routing/queue.js"; |
| 3 | +import type { OpenNextConfig } from "@opennextjs/aws/types/open-next"; |
| 4 | +import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js"; |
| 5 | +import { IgnorableError } from "@opennextjs/aws/utils/error.js"; |
| 6 | + |
| 7 | +import { getCloudflareContext } from "./cloudflare-context"; |
| 8 | + |
| 9 | +class ShardedD1TagCache implements NextModeTagCache { |
| 10 | + mode = "nextMode" as const; |
| 11 | + public readonly name = "sharded-d1-tag-cache"; |
| 12 | + |
| 13 | + private getDurableObjectStub(shardId: string) { |
| 14 | + const durableObject = getCloudflareContext().env.NEXT_CACHE_D1_SHARDED; |
| 15 | + if (!durableObject) throw new IgnorableError("No durable object binding for cache revalidation"); |
| 16 | + |
| 17 | + const id = durableObject.idFromName(shardId); |
| 18 | + return durableObject.get(id); |
| 19 | + } |
| 20 | + |
| 21 | + private generateShards(tags: string[]) { |
| 22 | + // For each tag, we generate a message group id |
| 23 | + const messageGroupIds = tags.map((tag) => ({ shardId: generateMessageGroupId(tag), tag })); |
| 24 | + // We group the tags by shard |
| 25 | + const shards = new Map<string, string[]>(); |
| 26 | + for (const { shardId, tag } of messageGroupIds) { |
| 27 | + const tags = shards.get(shardId) ?? []; |
| 28 | + tags.push(tag); |
| 29 | + shards.set(shardId, tags); |
| 30 | + } |
| 31 | + return shards; |
| 32 | + } |
| 33 | + |
| 34 | + private getConfig() { |
| 35 | + const cfEnv = getCloudflareContext().env; |
| 36 | + const db = cfEnv.NEXT_CACHE_D1_SHARDED; |
| 37 | + |
| 38 | + if (!db) debug("No Durable object found"); |
| 39 | + |
| 40 | + const isDisabled = !!(globalThis as unknown as { openNextConfig: OpenNextConfig }).openNextConfig |
| 41 | + .dangerous?.disableTagCache; |
| 42 | + |
| 43 | + if (!db || isDisabled) { |
| 44 | + return { isDisabled: true as const }; |
| 45 | + } |
| 46 | + |
| 47 | + return { |
| 48 | + isDisabled: false as const, |
| 49 | + db, |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + async hasBeenRevalidated(tags: string[], lastModified?: number): Promise<boolean> { |
| 54 | + const { isDisabled } = this.getConfig(); |
| 55 | + if (isDisabled) return false; |
| 56 | + const shards = this.generateShards(tags); |
| 57 | + let hasBeenRevalidated = false; |
| 58 | + console.log("Checking revalidation for tags", tags, shards); |
| 59 | + // We then create a new durable object for each shard |
| 60 | + await Promise.all( |
| 61 | + Array.from(shards.entries()).map(async ([shardId, shardedTags]) => { |
| 62 | + const stub = this.getDurableObjectStub(shardId); |
| 63 | + hasBeenRevalidated = hasBeenRevalidated || (await stub.hasBeenRevalidated(shardedTags, lastModified)); |
| 64 | + }) |
| 65 | + ); |
| 66 | + return hasBeenRevalidated; |
| 67 | + } |
| 68 | + |
| 69 | + async writeTags(tags: string[]): Promise<void> { |
| 70 | + const { isDisabled } = this.getConfig(); |
| 71 | + if (isDisabled) return; |
| 72 | + const shards = this.generateShards(tags); |
| 73 | + console.log("Writing tags", tags, shards); |
| 74 | + // We then create a new durable object for each shard |
| 75 | + await Promise.all( |
| 76 | + Array.from(shards.entries()).map(async ([shardId, shardedTags]) => { |
| 77 | + const stub = this.getDurableObjectStub(shardId); |
| 78 | + await stub.writeTags(shardedTags); |
| 79 | + }) |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export default new ShardedD1TagCache(); |
0 commit comments