-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclearUserCache.ts
More file actions
42 lines (38 loc) · 1.25 KB
/
clearUserCache.ts
File metadata and controls
42 lines (38 loc) · 1.25 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
36
37
38
39
40
41
42
"use server";
import { initContext, cacheKeyForPage } from "@/lib/chatHistory";
import { updateTag } from "next/cache";
import { getPagesList } from "@/lib/docs";
import { isCloudflare } from "@/lib/detectCloudflare";
import { headers } from "next/headers";
import { withServerActionInstrumentation } from "@sentry/nextjs";
export async function clearUserCacheAction() {
return withServerActionInstrumentation(
"clearUserCacheAction", // Action name for Sentry
{
headers: await headers(), // Connect client and server traces
recordResponse: true, // Include response data
},
async () => {
const ctx = await initContext();
if (!ctx.userId) return;
const pagesList = await getPagesList();
for (const lang of pagesList) {
for (const page of lang.pages) {
updateTag(
cacheKeyForPage({ lang: lang.id, page: page.slug }, ctx.userId)
);
}
}
if (isCloudflare()) {
const cache = await caches.open("chatHistory");
for (const lang of pagesList) {
for (const page of lang.pages) {
await cache.delete(
cacheKeyForPage({ lang: lang.id, page: page.slug }, ctx.userId)
);
}
}
}
}
);
}