-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Describe the bug
It looks like when useQuery reuses a cached query, refetchInterval starts at current time instead of time of last update. This means that actual refresh interval depends on end-user action - how they navigate the page.
Your minimal, reproducible example
https://codesandbox.io/p/sandbox/spring-pine-jm4py8
Steps to reproduce
- Wait until "Data age" reaches 5 seconds
- Click "2", which changes the queryKey
- Within 5 seconds (before first query expires) click "1", which changes queryKey back to original
- Data age will now reach age of between 15 and 20 seconds
Expected behavior
Because I set refreshInterval to 10 seconds, I expect query will be refreshed 10 seconds after previous execution, regardless if end user navigates the page and at some point gets cached query.
Using code like
let result = useQuery({ queryKey: [someParam], queryFn: () => { return {}; }, refetchInterval: 10000, staleTime: 10000, });
it doesn't make sense that this queryFn will refresh somewhere between 10 and 20 seconds, depending on end-user behavior.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
codesandbox.io
Tanstack Query adapter
react-query
TanStack Query version
5.51.1
TypeScript version
No response
Additional context
No response