From 43b8b38167cefec67e88e70351f7055c14324ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=AB=E3=82=99=E3=82=AA=E3=82=AB=E3=82=99=E3=82=AA?= Date: Tue, 18 Apr 2023 15:14:25 +0800 Subject: [PATCH] refactor: Remove the code in the client that disables the fetch cache by passing a request header, and replace it with server-side enforced fetch cache disabling. * fix last commit updateUsage should pass force argument not true. --- app/api/common.ts | 4 +--- app/components/settings.tsx | 2 +- app/requests.ts | 12 +++--------- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/app/api/common.ts b/app/api/common.ts index 2f7d4c3d3..d25cc1cd9 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -8,8 +8,6 @@ const BASE_URL = process.env.BASE_URL ?? OPENAI_URL; export async function requestOpenai(req: NextRequest) { const apiKey = req.headers.get("token"); const openaiPath = req.headers.get("path"); - const fetchCache = - req.headers.get("fetch-cache") == "enable" ? "default" : "no-store"; let baseUrl = BASE_URL; @@ -25,7 +23,7 @@ export async function requestOpenai(req: NextRequest) { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}`, }, - cache: fetchCache, + cache: "no-store", method: req.method, body: req.body, }); diff --git a/app/components/settings.tsx b/app/components/settings.tsx index 9f62d5017..3bde0089a 100644 --- a/app/components/settings.tsx +++ b/app/components/settings.tsx @@ -207,7 +207,7 @@ export function Settings(props: { closeSettings: () => void }) { const [loadingUsage, setLoadingUsage] = useState(false); function checkUsage(force: boolean = false) { setLoadingUsage(true); - updateStore.updateUsage(true).finally(() => { + updateStore.updateUsage(force).finally(() => { setLoadingUsage(false); }); } diff --git a/app/requests.ts b/app/requests.ts index e1bf33b58..59f8dd3c1 100644 --- a/app/requests.ts +++ b/app/requests.ts @@ -49,17 +49,15 @@ function getHeaders() { } export function requestOpenaiClient(path: string) { - return (body: any, method: string = "POST", fetchCache: boolean = true) => + return (body: any, method: string = "POST") => fetch("/api/openai?_vercel_no_cache=1", { method, headers: { "Content-Type": "application/json", path, ...getHeaders(), - "fetch-cache": fetchCache ? "enable" : "disable", }, body: body && JSON.stringify(body), - cache: fetchCache ? "default" : "no-store", //https://beta.nextjs.org/docs/data-fetching/fetching#dynamic-data-fetching }); } @@ -92,12 +90,8 @@ export async function requestUsage() { const [used, subs] = await Promise.all([ requestOpenaiClient( `dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`, - )(null, "GET", useFetchCache), - requestOpenaiClient("dashboard/billing/subscription")( - null, - "GET", - useFetchCache, - ), + )(null, "GET"), + requestOpenaiClient("dashboard/billing/subscription")(null, "GET"), ]); const response = (await used.json()) as {