Skip to content
Source
Sign inSign up
TanStack/query

Docs: special behaviour of `initialDataUpdatedAt=0` is not documented #7003

Send to my agent
ClosedJokero(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe the bug

It seems useQuery works differently from what is written in the docs when initialDataUpdatedAt=0:

This option allows the staleTime to be used for its original purpose, determining how fresh the data needs to be, while also allowing the data to be refetched on mount if the initialData is older than the staleTime

So if I have the following config, useQuery shouldn't refetch data on mount:

const { data } = useQuery({
    ...
    initialData: [],
    initialDataUpdatedAt: 0, // it shouldn't refetch data as Date.now() - 0 < Infinity, but it does refetch in reality
    staleTime: Infinity,
    ...
});

I guess this is by design as I found few issues/discussions where initialDataUpdatedAt: 0 was mentioned (for example, this and this).

However, there is no information about it on https://tanstack.com/query/v5/docs/framework/react/reference/useQuery and https://tanstack.com/query/v5/docs/framework/react/guides/initial-query-data.

Your minimal, reproducible example

https://stackblitz.com/edit/github-krgozk

Steps to reproduce

  1. Go to https://stackblitz.com/edit/github-krgozk
  2. Open Network tab in Dev Tools
  3. Find a request to https://api.github.com/repos/tannerlinsley/react-query (it shouldn't be made according to docs, as initialData is not older than staleTime)

Expected behavior

Case with initialDataUpdatedAt=0 is documented on https://tanstack.com/query/v5/docs/framework/react/reference/useQuery and https://tanstack.com/query/v5/docs/framework/react/guides/initial-query-data.

How often does this bug happen?

Every time

Screenshots or Videos

image

Platform

Chrome

Tanstack Query adapter

None

TanStack Query version

v5

TypeScript version

No response

Additional context

No response

  1. TkDodocommented

    it seems to be this condition specifically because zero is falsy in js 🙄

    https://github.com/TanStack/query/blob/b32ad24f5d2ac79eb8dee32047195e2bb9267b2b/packages/query-core/src/query.ts#L253

    It works if you set it to -1 for example.

    I would consider this a bug rather than a missing documentation. There a couple of those checks that we likely should change towards > 0 instead.

  2. okay, it's late. I think we shouldn't check for dataUpdatedAt here at all. This check likely means: we're always stale if there is no data yet. !dataUpatedAt kind of reflects that, but not really. The check should likely be made against this.state.data !== undefined like we do here:

    https://github.com/TanStack/query/blob/b32ad24f5d2ac79eb8dee32047195e2bb9267b2b/packages/query-core/src/query.ts#L624

  3. TkDodo closed this

  4. @TkDodo Ok, makes sense, thank you

Sign in to join this conversation.