From e95dc5f265625350e31fe6e8c918d31721c5caa7 Mon Sep 17 00:00:00 2001 From: JakeBreath Date: Thu, 17 Sep 2026 14:41:56 -0500 Subject: [PATCH] Clear the query cache when the signed-in account changes Logging out or switching accounts kept the previous user's React Query cache (follows, feed, cloud, e621 pages), so the new account briefly saw the old one's followed tags/pools until each query refetched. The query client now lives in lib/queryClient.ts and login/register/logout clear it alongside the e621 credential store. --- frontend/src/lib/queryClient.ts | 12 ++++++++++++ frontend/src/main.tsx | 14 ++------------ frontend/src/store/auth.ts | 7 +++++++ 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 frontend/src/lib/queryClient.ts diff --git a/frontend/src/lib/queryClient.ts b/frontend/src/lib/queryClient.ts new file mode 100644 index 0000000..998fe0f --- /dev/null +++ b/frontend/src/lib/queryClient.ts @@ -0,0 +1,12 @@ +import { QueryClient } from "@tanstack/react-query"; + +export const queryClient = new QueryClient({ + defaultOptions: { + queries: { + retry: 1, + staleTime: 30_000, + gcTime: 30 * 60_000, + refetchOnWindowFocus: false, + }, + }, +}); diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index f62492a..1ba7f81 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -1,21 +1,11 @@ -import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { QueryClientProvider } from "@tanstack/react-query"; import { StrictMode } from "react"; import { createRoot } from "react-dom/client"; import { BrowserRouter } from "react-router-dom"; import App from "./App"; import "./index.css"; - -const queryClient = new QueryClient({ - defaultOptions: { - queries: { - retry: 1, - staleTime: 30_000, - gcTime: 30 * 60_000, - refetchOnWindowFocus: false, - }, - }, -}); +import { queryClient } from "@/lib/queryClient"; createRoot(document.getElementById("root")!).render( diff --git a/frontend/src/store/auth.ts b/frontend/src/store/auth.ts index cd79ad7..d3d1ccb 100644 --- a/frontend/src/store/auth.ts +++ b/frontend/src/store/auth.ts @@ -1,6 +1,7 @@ import { create } from "zustand"; import { api, getToken, setToken } from "@/lib/api"; +import { queryClient } from "@/lib/queryClient"; import type { User } from "@/lib/types"; import { useE621 } from "@/store/e621"; @@ -24,6 +25,9 @@ export const useAuth = create((set) => ({ }); setToken(data.token); const user = await api("/api/auth/me/"); + // Never show the previous account's cached data to this one. + queryClient.clear(); + useE621.getState().clear(); set({ user, ready: true }); }, @@ -33,6 +37,8 @@ export const useAuth = create((set) => ({ json: { username, password }, }); setToken(data.token); + queryClient.clear(); + useE621.getState().clear(); set({ user: data.user, ready: true }); }, @@ -43,6 +49,7 @@ export const useAuth = create((set) => ({ // The token may already be invalid; clearing it locally is enough. } setToken(null); + queryClient.clear(); useE621.getState().clear(); set({ user: null, ready: true }); },