Skip to content

Commit

Permalink
Only return cached data if not expired, adjust handleSetEnabled logic
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbaroni committed Jan 4, 2025
1 parent ec8eb14 commit e838acd
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/state/internal/createQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,9 @@ export function createQueryStore<
getData(params?: TParams) {
if (disableCache) return null;
const currentQueryKey = params ? getQueryKey(params) : get().queryKey;
return get().queryCache[currentQueryKey]?.data ?? null;
const cacheEntry = get().queryCache[currentQueryKey];
const isExpired = !!cacheEntry?.lastFetchedAt && Date.now() - cacheEntry.lastFetchedAt > cacheTime;
return isExpired ? null : cacheEntry?.data ?? null;
},

getStatus() {
Expand Down Expand Up @@ -802,10 +804,8 @@ export function createQueryStore<
if (state.enabled) {
const currentParams = getCurrentResolvedParams();
const currentKey = state.queryKey;
if (currentKey !== lastFetchKey) {
state.fetch(currentParams, { force: true });
} else if (!state.queryCache[currentKey] || state.isStale()) {
state.fetch();
if (currentKey !== lastFetchKey || !state.queryCache[currentKey] || state.isStale()) {
state.fetch(currentParams, undefined);
} else {
scheduleNextFetch(currentParams, undefined);
}
Expand Down

0 comments on commit e838acd

Please sign in to comment.