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

Skip fetch when initialData is set on ensureQueryData #8418

Closed
Alan-Gomes opened this issue Dec 8, 2024 · 1 comment · Fixed by #8425
Closed

Skip fetch when initialData is set on ensureQueryData #8418

Alan-Gomes opened this issue Dec 8, 2024 · 1 comment · Fixed by #8425

Comments

@Alan-Gomes
Copy link

Alan-Gomes commented Dec 8, 2024

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

@TkDodo
Copy link
Collaborator

TkDodo commented Dec 10, 2024

good point. we need to fix that.

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

Successfully merging a pull request may close this issue.

2 participants