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
- Wrap any app in PersistQueryClientProvider with an indexdb persister example from the docs
- Add a custom
queryKeyHashFnto queryClientdefaultOptions.queries.queryKeyHashFn. E.g. return(queryKey)=>hashKey(queryKey) + "_test" - 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
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
hashQueryKeyByOptionsagainst all the items of the queryCache, but instead it’s rehashing the queryKey filter based on the options of all the queries in cache, soo(n), which in most cases should beo(1), unless there’s a customhashQueryKeyByOptionsdefined 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.queryHashdoesn’t pick up the cached query, andqueryStatereturnsundefined, which in turn breaksgetQueryStatusLabelthat expectsqueryStateto exist.To fix, we could just fall back to
QueryRowcomponent prop’squery.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