From 89cf66f28abdbdc88c9543f644aab257a39f1194 Mon Sep 17 00:00:00 2001 From: Senko-san Date: Sun, 14 Jun 2026 14:13:59 +0300 Subject: [PATCH] fix(api): refetch from server when online (stale-while-revalidate) The Tier-2 rehydrated cache seeded fulfilled entries at startup, so RTKQ served stale data and never hit the network (manual metadata edits, etc. only appeared after clearing the cache). Enable refetchOnMountOrArgChange + refetchOnReconnect + refetchOnFocus and wire setupListeners, so the cached snapshot still shows instantly but the server silently revalidates it whenever reachable. Co-Authored-By: Claude Opus 4.8 --- src/api/index.ts | 9 +++++++++ src/store/index.ts | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/src/api/index.ts b/src/api/index.ts index f9a4bdd..b24149c 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -5,6 +5,15 @@ import { REHYDRATE_API, type RehydrateApiPayload } from './rehydrate'; export const api = createApi({ reducerPath: 'api', baseQuery: baseQueryWithReauth, + // Stale-while-revalidate. The Tier-2 rehydrated cache (below) seeds fulfilled + // entries at startup, which would otherwise make RTKQ serve stale data and + // never hit the network. These flags keep showing the cached snapshot + // instantly but silently refetch from the server whenever it's reachable — + // on mount/arg change, on reconnect, and on window refocus. The result: the + // server is the source of truth when online; the cache is only a fallback. + refetchOnMountOrArgChange: true, + refetchOnReconnect: true, + refetchOnFocus: true, tagTypes: [ 'Track', 'Album', diff --git a/src/store/index.ts b/src/store/index.ts index 46fcd26..207e079 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,4 +1,5 @@ import { configureStore } from '@reduxjs/toolkit'; +import { setupListeners } from '@reduxjs/toolkit/query'; import { api } from '../api'; import authReducer from './slices/auth'; import connectionReducer from './slices/connection'; @@ -27,6 +28,10 @@ export const store = configureStore({ getDefaultMiddleware().concat(api.middleware), }); +// Enable refetchOnReconnect / refetchOnFocus by dispatching the browser's +// online + focus events into RTKQ (no-op without the api flags set in api/index). +setupListeners(store.dispatch); + // Flush queue/player changes back to localStorage (throttled). startPersistence(store);