From 093e47cb0a80153513ab7daa88ea4f1a461c80cb Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 11 Jul 2025 18:19:08 +0800 Subject: [PATCH] feat: add fetch cache no-store and header cache-control no-store --- apps/desktop/layer/main/src/lib/api-client.ts | 1 + apps/desktop/layer/renderer/src/lib/api-fetch.ts | 6 ++---- apps/mobile/src/lib/api-fetch.ts | 6 ++---- apps/mobile/src/lib/auth.ts | 1 + apps/ssr/client/lib/api-fetch.ts | 1 + apps/ssr/client/lib/auth.ts | 1 + apps/ssr/src/lib/api-client.ts | 1 + packages/internal/shared/package.json | 1 - packages/internal/shared/src/auth.ts | 11 +---------- packages/internal/store/package.json | 1 - packages/internal/store/src/user/store.ts | 9 +++------ packages/internal/utils/src/headers.ts | 7 +++++++ pnpm-lock.yaml | 6 ------ 13 files changed, 20 insertions(+), 32 deletions(-) diff --git a/apps/desktop/layer/main/src/lib/api-client.ts b/apps/desktop/layer/main/src/lib/api-client.ts index 41ae4c22d..f52514d03 100644 --- a/apps/desktop/layer/main/src/lib/api-client.ts +++ b/apps/desktop/layer/main/src/lib/api-client.ts @@ -16,6 +16,7 @@ const apiFetch = ofetch.create({ credentials: "include", signal: abortController.signal, retry: false, + cache: "no-store", onRequest({ request }) { logger.info(`API Request: ${request.toString()}`) }, diff --git a/apps/desktop/layer/renderer/src/lib/api-fetch.ts b/apps/desktop/layer/renderer/src/lib/api-fetch.ts index b862302b4..c48ce9961 100644 --- a/apps/desktop/layer/renderer/src/lib/api-fetch.ts +++ b/apps/desktop/layer/renderer/src/lib/api-fetch.ts @@ -20,6 +20,7 @@ export const apiFetch = ofetch.create({ baseURL: env.VITE_API_URL, credentials: "include", retry: false, + cache: "no-store", onRequest: ({ options }) => { const header = new Headers(options.headers) @@ -85,10 +86,7 @@ export const apiFetch = ofetch.create({ export const apiClient = hc(env.VITE_API_URL, { fetch: async (input, options = {}) => - apiFetch(input.toString(), { - ...options, - cache: "no-store", - }).catch((err) => { + apiFetch(input.toString(), options).catch((err) => { if (err instanceof FetchError && !err.response) { setApiStatus(NetworkStatus.OFFLINE) } diff --git a/apps/mobile/src/lib/api-fetch.ts b/apps/mobile/src/lib/api-fetch.ts index 91dc48337..5dd8af8a8 100644 --- a/apps/mobile/src/lib/api-fetch.ts +++ b/apps/mobile/src/lib/api-fetch.ts @@ -19,6 +19,7 @@ export const apiFetch = ofetch.create({ retry: false, credentials: "omit", baseURL: proxyEnv.API_URL, + cache: "no-store", onRequest: async (ctx) => { const { options, request } = ctx if (__DEV__) { @@ -78,10 +79,7 @@ export const apiFetch = ofetch.create({ export const apiClient = hc(proxyEnv.API_URL, { fetch: async (input: any, options = {}) => - apiFetch(input.toString(), { - ...options, - cache: "no-store", - }).catch((err) => { + apiFetch(input.toString(), options).catch((err) => { throw err }), async headers() { diff --git a/apps/mobile/src/lib/auth.ts b/apps/mobile/src/lib/auth.ts index 8ecda4cfb..64053ac16 100644 --- a/apps/mobile/src/lib/auth.ts +++ b/apps/mobile/src/lib/auth.ts @@ -49,6 +49,7 @@ const plugins = [ export const authClient = createAuthClient({ baseURL: `${proxyEnv.API_URL}/better-auth`, fetchOptions: { + cache: "no-store", // Learn more: https://better-fetch.vercel.app/docs/hooks onRequest: async (ctx) => { const headers = createMobileAPIHeaders({ diff --git a/apps/ssr/client/lib/api-fetch.ts b/apps/ssr/client/lib/api-fetch.ts index 591aaf2aa..70e35481e 100644 --- a/apps/ssr/client/lib/api-fetch.ts +++ b/apps/ssr/client/lib/api-fetch.ts @@ -9,6 +9,7 @@ import PKG from "../../../desktop/package.json" const apiFetch = ofetch.create({ credentials: "include", retry: false, + cache: "no-store", onRequest: ({ options }) => { const header = new Headers(options.headers) diff --git a/apps/ssr/client/lib/auth.ts b/apps/ssr/client/lib/auth.ts index 1b1925db0..3f74cafda 100644 --- a/apps/ssr/client/lib/auth.ts +++ b/apps/ssr/client/lib/auth.ts @@ -11,6 +11,7 @@ const auth = new Auth({ webURL: env.VITE_WEB_URL, fetchOptions: { headers, + cache: "no-store", }, }) diff --git a/apps/ssr/src/lib/api-client.ts b/apps/ssr/src/lib/api-client.ts index cde108142..62c0205e8 100644 --- a/apps/ssr/src/lib/api-client.ts +++ b/apps/ssr/src/lib/api-client.ts @@ -33,6 +33,7 @@ export const createApiFetch = () => { return ofetch.create({ credentials: "include", retry: false, + cache: "no-store", onRequest(context) { if (__DEV__) console.info(`request: ${context.request}`) diff --git a/packages/internal/shared/package.json b/packages/internal/shared/package.json index 2d100ece6..58befecbc 100644 --- a/packages/internal/shared/package.json +++ b/packages/internal/shared/package.json @@ -40,7 +40,6 @@ "better-auth": "1.2.9", "drizzle-orm": "0.44.2", "hono": "4.8.1", - "nanoid": "5.1.5", "sonner": "2.0.6", "stripe": "18.2.1", "zod": "3.25.75" diff --git a/packages/internal/shared/src/auth.ts b/packages/internal/shared/src/auth.ts index a94dd1df4..49479f2ec 100644 --- a/packages/internal/shared/src/auth.ts +++ b/packages/internal/shared/src/auth.ts @@ -4,7 +4,6 @@ import type { authPlugins } from "@follow/shared/hono" import type { BetterAuthClientPlugin, BetterFetchOption } from "better-auth/client" import { inferAdditionalFields, twoFactorClient } from "better-auth/client/plugins" import { createAuthClient } from "better-auth/react" -import { nanoid } from "nanoid" type AuthPlugin = (typeof authPlugins)[number] export const baseAuthPlugins = [ @@ -58,21 +57,13 @@ export class Auth { plugins: baseAuthPlugins, fetchOptions: { ...this.options.fetchOptions, - + cache: "no-store", onRequest: (context) => { const referralCode = localStorage.getItem(getStorageNS("referral-code")) if (referralCode) { context.headers.set("folo-referral-code", referralCode) } - if (context.query) { - context.query.v = nanoid(8) - } else { - context.query = { - v: nanoid(8), - } - } - this.options.fetchOptions?.onRequest?.(context) return context diff --git a/packages/internal/store/package.json b/packages/internal/store/package.json index c7aa67227..dbbf4a267 100644 --- a/packages/internal/store/package.json +++ b/packages/internal/store/package.json @@ -124,7 +124,6 @@ "@tanstack/react-query": "5.81.5", "es-toolkit": "1.39.6", "immer": "10.1.1", - "nanoid": "5.1.5", "zustand": "5.0.6" } } diff --git a/packages/internal/store/src/user/store.ts b/packages/internal/store/src/user/store.ts index 89b8384b6..fe7374df2 100644 --- a/packages/internal/store/src/user/store.ts +++ b/packages/internal/store/src/user/store.ts @@ -3,7 +3,6 @@ import type { UserSchema } from "@follow/database/schemas/types" import { UserService } from "@follow/database/services/user" import type { AuthSession } from "@follow/shared/hono" import { create, indexedResolver, windowScheduler } from "@yornaath/batshit" -import { nanoid } from "nanoid" import { apiClient, authClient } from "../context" import type { Hydratable, Resetable } from "../internal/base" @@ -67,11 +66,9 @@ class UserSyncService { }) async whoami() { - const res = (await (apiClient()["better-auth"] as any)["get-session"].$get({ - query: { - v: nanoid(8), - }, - })) as AuthSession | null + const res = (await (apiClient()["better-auth"] as any)[ + "get-session" + ].$get()) as AuthSession | null if (res) { const user = honoMorph.toUser(res.user, true) immerSet((state) => { diff --git a/packages/internal/utils/src/headers.ts b/packages/internal/utils/src/headers.ts index e7c1b3ce1..8e86a4a7b 100644 --- a/packages/internal/utils/src/headers.ts +++ b/packages/internal/utils/src/headers.ts @@ -50,6 +50,10 @@ export const createBuildSafeHeaders = return headers } +const commonHeaders = { + "Cache-Control": "no-store", +} + enum DesktopPlatform { Desktop = "desktop", DesktopWeb = "desktop/web", @@ -92,6 +96,7 @@ export const createDesktopAPIHeaders = ({ version }: { version: string }) => { } return { + ...commonHeaders, ...(platform ? { "X-App-Platform": platform } : {}), "X-App-Name": "Folo Web", "X-App-Version": version, @@ -105,6 +110,7 @@ enum SSRPlatform { export const createSSRAPIHeaders = ({ version }: { version: string }) => { return { + ...commonHeaders, "X-App-Platform": SSRPlatform.SSR, "X-App-Name": "Folo SSR", "X-App-Version": version, @@ -148,6 +154,7 @@ export const createMobileAPIHeaders = ({ } return { + ...commonHeaders, ...(platform ? { "X-App-Platform": platform } : {}), "X-App-Name": "Folo Mobile", "X-App-Version": version, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index db7b63848..62be38d94 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1697,9 +1697,6 @@ importers: hono: specifier: 4.8.1 version: 4.8.1(patch_hash=5c74c2d2afaa5880c13d75458dd26c84da568851691b1fb6de6d877c29936d05) - nanoid: - specifier: 5.1.5 - version: 5.1.5 sonner: specifier: 2.0.6 version: 2.0.6(react-dom@19.0.0(react@19.0.0))(react@19.0.0) @@ -1742,9 +1739,6 @@ importers: immer: specifier: 10.1.1 version: 10.1.1(patch_hash=594c60b929bc0a3b56576f1a1787da1aec2a1fba51e3c21ec09c0ed38280af6c) - nanoid: - specifier: 5.1.5 - version: 5.1.5 react: specifier: 19.0.0 version: 19.0.0