-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathindex.ts
More file actions
35 lines (31 loc) · 1.15 KB
/
index.ts
File metadata and controls
35 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { error } from "@opennextjs/aws/adapters/logger.js";
import type { CDNInvalidationHandler } from "@opennextjs/aws/types/overrides.js";
import { getCloudflareContext } from "../../cloudflare-context.js";
import { debugCache, internalPurgeCacheByTags } from "../internal.js";
interface PurgeOptions {
type: "durableObject" | "direct";
}
export const purgeCache = ({ type = "direct" }: PurgeOptions): CDNInvalidationHandler => {
return {
name: "cloudflare",
async invalidatePaths(paths) {
const { env } = getCloudflareContext();
const tags = paths.map((path) => `_N_T_${path.rawPath}`);
debugCache("cdnInvalidation", "Invalidating paths:", tags);
if (type === "direct") {
await internalPurgeCacheByTags(env, tags);
} else {
const durableObject = env.NEXT_CACHE_DO_PURGE;
if (!durableObject) {
error("Purge cache: NEXT_CACHE_DO_PURGE not found. Skipping cache purge.");
return;
}
const id = durableObject.idFromName("cache-purge");
const obj = durableObject.get(id);
await obj.purgeCacheByTags(tags);
}
debugCache("cdnInvalidation", "Invalidated paths:", tags);
},
};
};
export default purgeCache;