Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polling Stops with refetchIntervalInBackground and Retry When Tab Is Inactive #8353

Open
antoantonydh opened this issue Nov 26, 2024 · 0 comments

Comments

@antoantonydh
Copy link

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

  1. Open codesandbox link and use the preview option in new tab(https://gk6c6t.csb.app/)
  2. open chrome network tool to set the network as offline
  3. 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)
  4. switch back to the preview tab
  5. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant