Skip to content

getQueryData returns undefined even if the persisted cache is there #7204

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

Closed
frederikhors opened this issue Mar 30, 2024 · 2 comments
Closed

Comments

@frederikhors
Copy link
Contributor

frederikhors commented Mar 30, 2024

I'm using experimental_createPersister for IndexedDB cache.

Until a few minutes ago I was convinced that I could use method queryClient.getQueryData to get an element from the persisted cache even without the "momentary" cache being full.

I express myself with the terms "persisted" and "momentary" but I don't know if they are right.

I created a small but effective reproduction of the problem here.

Steps to reproduce:

  1. Open the first page of the app:

    image

  2. Click on "John" and on "Bob" links

  3. The "persisted" cache and the "momentary" cache are filled now

  4. Go to the Home page again

  5. RELOAD THE PAGE

  6. Click on the button "Try to get Player 2 from the cache"

  7. See the message in console: undefined, DAMN!

Why?

If you open the browser Dev Tools you can see in the IndexedDB the cache is there, filled up!

Why is Bob undefined?

Is getQueryData getting only from the "momentary" cache?

@TkDodo
Copy link
Collaborator

TkDodo commented Apr 2, 2024

Is getQueryData getting only from the "momentary" cache?

essentially, yes. getQueryData and setQueryData are synchronous functions that read from the in-memory cache. Thus, they cannot read from a persistent cache, because that would per definition be asynchronous.

This is essentially the same reason why setQueryData doesn't persist data, so it's related to:

If you want immediate cache restoration, please use the PersistQueryClientProvider. the createPersister api is meant for lazily filling the queryCache at the first time when the queryFn runs. You can run it with queryClient.ensureQueryData(). This will read from the in-memory cache, and run the queryFn (thus reading from the persistent cache) if there is no data loaded yet.

You can't have the cake and eat it, too :)

@TkDodo TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale Apr 2, 2024
@frederikhors
Copy link
Contributor Author

If you want immediate cache restoration, please use the PersistQueryClientProvider.

Can I use it with Svelte too?

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

2 participants