Skip to content

Using refreshInterval and staleTime can cause refresh to only occur after refetchInterval + staleTime  #7721

@nramovs-sr

Description

@nramovs-sr

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

  1. Wait until "Data age" reaches 5 seconds
  2. Click "2", which changes the queryKey
  3. Within 5 seconds (before first query expires) click "1", which changes queryKey back to original
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions