Skip to content

Commit 56a93c8

Browse files
committed
Set "cache-tag" on the regional cache entries
1 parent 9adc3a3 commit 56a93c8

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

packages/cloudflare/src/api/overrides/incremental-cache/regional-cache.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,18 @@ class RegionalCache implements IncrementalCache {
143143
? ONE_MINUTE_IN_SECONDS
144144
: entry.value.revalidate || THIRTY_MINUTES_IN_SECONDS;
145145

146+
const tags = getTagsFromCacheEntry(entry);
146147
await cache.put(
147148
key,
148149
new Response(JSON.stringify(entry), {
149-
headers: new Headers({ "cache-control": `max-age=${age}` }),
150+
headers: new Headers({
151+
"cache-control": `max-age=${age}`,
152+
...(tags.length > 0
153+
? {
154+
"cache-tag": tags.join(","),
155+
}
156+
: {}),
157+
}),
150158
})
151159
);
152160
}
@@ -175,3 +183,25 @@ class RegionalCache implements IncrementalCache {
175183
export function withRegionalCache(cache: IncrementalCache, opts: Options) {
176184
return new RegionalCache(cache, opts);
177185
}
186+
187+
/**
188+
* Extract the list of tags from a cache entry.
189+
*/
190+
export function getTagsFromCacheEntry(entry: IncrementalCacheEntry<boolean>): string[] {
191+
if ("tags" in entry.value && entry.value.tags) {
192+
return entry.value.tags;
193+
}
194+
195+
if (
196+
"meta" in entry.value &&
197+
entry.value.meta &&
198+
"headers" in entry.value.meta &&
199+
entry.value.meta.headers
200+
) {
201+
const rawTags = entry.value.meta.headers["x-next-cache-tags"];
202+
if (typeof rawTags === "string") {
203+
return rawTags.split(",");
204+
}
205+
}
206+
return [];
207+
}

0 commit comments

Comments
 (0)