49 lines
1.8 KiB
TypeScript
49 lines
1.8 KiB
TypeScript
import { QueryClient, InvalidateQueryFilters } from '@tanstack/react-query';
|
|
import { bumpMutationEpoch } from '../services/httpClient';
|
|
|
|
export const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
// Remove caching from all pages: zero stale time and zero cache retention
|
|
// Whenever a component needs data, it must call its corresponding endpoint(s)
|
|
staleTime: 0,
|
|
gcTime: 0,
|
|
refetchOnMount: 'always',
|
|
refetchOnWindowFocus: 'always',
|
|
refetchOnReconnect: 'always',
|
|
retry: 1,
|
|
},
|
|
},
|
|
});
|
|
|
|
// Retain caching ONLY for sync status, auth token, roles and grants
|
|
const RETAINED_CACHE_OPTIONS = {
|
|
staleTime: 5 * 60 * 1000,
|
|
gcTime: 10 * 60 * 1000,
|
|
refetchOnMount: false,
|
|
refetchOnWindowFocus: false,
|
|
};
|
|
|
|
queryClient.setQueryDefaults(['roles'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['system-roles'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['role-claims'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['user-roles'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['user-claims'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['grants'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['user-grants'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['user-sync'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['sync-status'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['auth'], RETAINED_CACHE_OPTIONS);
|
|
queryClient.setQueryDefaults(['auth-token'], RETAINED_CACHE_OPTIONS);
|
|
|
|
/**
|
|
* Invalidates queries while simultaneously bumping the mutation epoch.
|
|
*/
|
|
export async function invalidateAndBustCache(
|
|
client: QueryClient,
|
|
filters?: InvalidateQueryFilters
|
|
): Promise<void> {
|
|
bumpMutationEpoch();
|
|
await client.invalidateQueries(filters);
|
|
}
|