query: Devtools + async persistence + custom queryKeyHashFn doesn't work

Describe the bug

When using both persistence and a custom queryKeyHashFn, the DevTools will crash on initial load with error:

TypeError: Cannot read properties of undefined (reading 'fetchStatus')
    at getQueryStatusColor

Busting the cache on each load “fixes” it. And so does removing the custom queryKeyHashFn.

Just upgraded from v4, and this worked fine then. I also tried with a createSyncStoragePersister, and that works completely fine as well.

Your minimal, reproducible example

https://codesandbox.io/p/devbox/musing-williamson-vgj34y?file=%2Fsrc%2FApp.jsx%3A90%2C29

Steps to reproduce

  1. Wrap any app in PersistQueryClientProvider with an indexdb persister example from the docs
  2. Add a custom queryKeyHashFn to queryClient defaultOptions.queries.queryKeyHashFn. E.g. return (queryKey)=>hashKey(queryKey) + "_test"
  3. Reload the page

It works when there’s no cache being loaded. It doesn’t when something is loaded from the cache.

Expected behavior

Devtools should support a custom queryKeyHashFn with async persistence.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Macos, latest chrome

Tanstack Query adapter

react-query

TanStack Query version

5.22.2

TypeScript version

No response

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 2
  • Comments: 15

Most upvoted comments

Ah, you’re absolutely right. I misunderstood the code / was thrown off by the console logs – sorry for my misunderstanding. I thought it was calling hashQueryKeyByOptions against all the items of the queryCache, but instead it’s rehashing the queryKey filter based on the options of all the queries in cache, so o(n), which in most cases should be o(1), unless there’s a custom hashQueryKeyByOptions defined for the query (rare if any cases usually). But in practice it probably (hopefully) gets JIT’ed away and not have a significant performance penalty anyways.

So, the problem really only exists on Devtools level.

(e) => e.query.queryHash === props.query.queryHash doesn’t pick up the cached query, and queryState returns undefined, which in turn breaks getQueryStatusLabel that expects queryState to exist.

To fix, we could just fall back to QueryRow component prop’s query.state, which by definition has to be defined. Happy to create a PR, but I have never worked with Solid before personally, and don’t quite understand why there needs to be 4 subsequent calls to the queryCache.find to read the properties of the same query. Would be much easier to add the fallback otherwise (in 1 place, instead of 4, and 3 of which already have nullish coalescing).

Alternatively, a more elegant fix potentially would be a function to find a query in the cache based on the queryHash rather than the queryKey. But not sure if that should exist only on the devtools level or would also be useful in query-core.

I updated the reproduction, and it should break more reliably now (refresh the preview and check the console + query list will be empty even though the counter at the top shows otherwise): https://codesandbox.io/p/devbox/musing-williamson-vgj34y