1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-27 11:02:16 +01:00
unleash.unleash/frontend/src/hooks/useClearSWRCache.ts
2024-02-22 14:37:08 +01:00

15 lines
529 B
TypeScript

import { useSWRConfig } from 'swr';
/**
With dynamic search and filter parameters we want to prevent cache from growing extensively.
We only keep the latest cache key `currentKey` and remove all other entries identified
by the `clearPrefix`
*/
export const useClearSWRCache = (currentKey: string, clearPrefix: string) => {
const { cache } = useSWRConfig();
const keys = [...cache.keys()];
keys.filter((key) => key !== currentKey && key.startsWith(clearPrefix)).map(
(key) => cache.delete(key),
);
};