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.
This commit is contained in:
ガオガオ 2023-04-18 15:14:25 +08:00
parent d2ee1bd212
commit 43b8b38167
3 changed files with 5 additions and 13 deletions

View File

@ -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,
});

View File

@ -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);
});
}

View File

@ -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 {