Polling Stops with refetchIntervalInBackground and Retry When Tab Is Inactive #8353
Describe the bug#
I am creating this issue based on the discussion here.
When using refetchIntervalInBackground with retry, the retry mechanism fails to execute if the browser tab is inactive. I have the following configuration for my React Query setup:
const client = new QueryClient({
defaultOptions: {
queries: {
staleTime: 30_000, // 30 sec
networkMode: "always",
},
mutations: {
networkMode: "always",
},
},
});
export function useGetTodoList() {
return useQuery(
["todos"],
async () => {
// fetch logic here
},
{
refetchInterval: 60_000, // 1 min
refetchIntervalInBackground: true,
retry: 3,
}
);
}
Problem#
With this configuration:
- The query is set to poll every 1 minute (
refetchInterval: 60_000), even when the tab is inactive (refetchIntervalInBackground: true). - The retry mechanism is configured to retry 3 times on failure (
retry: 3).
However, if the tab is inactive and a request fails, the retry logic pauses and waits until the tab becomes active again. This behavior eliminates the purpose of refetchIntervalInBackground, as the retries do not proceed while the tab remains inactive.
Your minimal, reproducible example#
https://codesandbox.io/p/sandbox/suspicious-butterfly-gk6c6t
Steps to reproduce#
- Open codesandbox link and use the preview option in new tab(https://gk6c6t.csb.app/)
- open chrome network tool to set the network as offline
- switch to next tab and wait for 3 minutes, so we can expect atleast (3 requests for refetch + retry calls (3 * 3 = 9). so total of 12 requests atleast)
- switch back to the preview tab
- there will be only 2 failed requests instead of ~12
Expected behavior#
When refetchIntervalInBackground: true is used:
- Retries should execute in the background, independent of tab visibility.
- The retry mechanism should adhere to the configured retry settings (e.g., retry count, delay) without relying on the tab being active.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
Browser: Chrome
Tanstack Query adapter#
react-query
TanStack Query version#
4.36.1
TypeScript version#
No response
Additional context#
No response
birkskyum(GitHub profile, opens in a new tab) added labelspackage: react-query
option 1 might lead to situations where when a query errors, it was paused before, but it will now just go on fetching without a way to prevent that.
I like option 1 — that seems like the correct behavior for refetch logic with intervals. We probably don’t need to pause the query after an error in this case. It makes sense for refetching to continue on the configured interval, regardless of whether the previous request failed.
yeah I agree, let’s remove it. Do you want to PR it?
TkDodo removed labelspackage: react-query
TkDodo added labelspackage: query-core
Fix PR: https://github.com/TanStack/query/pull/9563 PTAL 👍
we might do this in v6 but in the meantime, it’s documented that this is on purpose
TkDodo closed this
Sign in to join this conversation.
The thing is that currently, retries are always paused when the window is not focused:
https://github.com/TanStack/query/blob/18953be6f61553f4ed3dd888945dfa1683aa6e40/packages/query-core/src/retryer.ts#L97-L100
We could check for
refetchIntervalInBackgroundhere as well, like we do here:https://github.com/TanStack/query/blob/5d69ad773a7a2df632df48eefce6fd9772d26db7/packages/query-core/src/queryObserver.ts#L406-L407
However, retries are different from interval fetches. I don’t think
refetchIntervalInBackgroundshould influenceretryfetches at all. So the options are:refetchIntervalInBackgroundtofetchInBackgroundand make all fetches, including retries, depend on that flag.option 2 is a breaking change, but we could deprecate
refetchIntervalInBackgroundand introduce the new flagfetchInBackground. One problematic thing is that if you have window focus refetching turned offoption 1 might lead to situations where when a query errors, it was paused before, but it will now just go on fetching without a way to prevent that.
Not sure what is better 😅