Skip to content
Source
Sign inSign up
TanStack/query

Skip fetch when `initialData` is set on `ensureQueryData` #8418

ClosedAlan-Gomes(GitHub profile, opens in a new tab) opened this issue 1 comment

Describe the bug

Firstly, thank you for your work on this library! I am currently using a pull approach to seed the query cache, and I have encountered a specific issue. When a user enters a page, I call ensureQueryData, passing in an initialData value, which may be undefined. The issue I am facing is that ensureQueryData always triggers a fetch when the data is not already in the cache, even when initialData is provided (non undefined).

Below is a simplified example in pseudo-code to illustrate the issue:

  const query = queries.postById.getOptions(id);

  await queryClient.ensureQueryData({
    ...query,
    // here I search inside list queries to find an existing post to be on the initial data
    // even when I have a post, the fetch is executed
    initialData: utils.findInPages(queries.postList.getKey(), byId(id)),
  });

Your minimal, reproducible example

Stackblitz

Steps to reproduce

https://stackblitz.com/edit/vitejs-vite-cnkcntdk?file=src%2FApp.tsx

Expected behavior

Since I am providing initialData, the expected behavior is for the query client to store the initialData in the cache and bypass the fetch process.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome 131

Tanstack Query adapter

react-query

TanStack Query version

5.62.3

TypeScript version

No response

Additional context

No response

  1. good point. we need to fix that.

  2. TkDodo added labelspackage: query-core

  3. TkDodo closed this

Sign in to join this conversation.