You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
separate shouldLoadOnMount and shouldFetchOnMount #4368
note:
work in progress. im working on a fix, will PR later
so no repro code here
edit:
this may be a race condition
between my persister.restoreClient and QueryObserver.onSubscribe.
all queries should wait for restoreClient
use case:
im fetching immutable data: files from github.
these files are content-addressed via commit hash or blob hash,
so building a cache should be trivial
goal:
fetch every file only once
how:
persistence via indexeddb
query options:
{cacheTime: Infinity,staleTime: Infinity,refetchOnWindowFocus: false,refetchOnMount: false,refetchOnReconnect: false,// any previous data will be kept when fetching new data because the query key changed.//keepPreviousData: true, // ?}
but this is not enough to prevent TanStack/query from refetching
executeFetch is still called in QueryObserver.onSubscribe
classQueryObserverextendsSubscribable{// ...onSubscribe(){if(this.listeners.length===1){this.currentQuery.addObserver(this);if(shouldFetchOnMount(this.currentQuery,this.options)){// FIXME QueryObserver.onSubscribe should not refetch, but use cachethis.executeFetch();}this.updateTimers();}}
when i remove the line this.executeFetch(); it works as expected = dont refetch, use cache
problem: shouldFetchOnMount calls shouldLoadOnMount
but "fetch" and "load" are different actions
im wrapping my app in PersistQueryClientProvider
which is defined in tanstack-solid-query-persist-client.jsx
using the same logic as similar components, see "based on"
by fetching from the github api
i get an exact number of the actual fetches in the x-ratelimit-used header
pause the queryClient before restoreClient, so that all queries wait until the persistent cache is loaded. without patching, QueryObserver.onSubscribe calls QueryObserver.executeFetch too early
fix @tanstack+solid-query error DOMException: Failed to execute 'put' on 'IDBObjectStore': [object Array] could not be cloned. because Proxy objects in queryKey cannot be serialized for indexeddb
imho these fix bugs in tanstack, but maybe im doing something wrong...?
Activity
milahu commentedon Oct 24, 2022
probably yes ... closing for #3198 (comment)
TkDodo commentedon Oct 24, 2022
they do if you use the
PersistQueryClientProvidermilahu commentedon Oct 24, 2022
see my demo app prosemirror-inplace-editing-demo based on solid.js framework
im wrapping my app in
PersistQueryClientProviderwhich is defined in tanstack-solid-query-persist-client.jsx
using the same logic as similar components, see "based on"
by fetching from the github api
i get an exact number of the actual fetches in the
x-ratelimit-usedheaderi need patches to
QueryObserver.onSubscribecallsQueryObserver.executeFetchtoo early@tanstack+solid-queryerrorDOMException: Failed to execute 'put' on 'IDBObjectStore': [object Array] could not be cloned.because Proxy objects in queryKey cannot be serialized for indexeddbimho these fix bugs in tanstack, but maybe im doing something wrong...?
TkDodo commentedon Oct 24, 2022
this is what the Provider is doing via the
isRestoringvalue that is passed with theIsRestoringProvider:query/packages/react-query-persist-client/src/PersistQueryClientProvider.tsx
Line 50 in 7741a2f
Observers are not subscribed while the client is still restoring, so no fetches should go out. We have tests for that :)
milahu commentedon Oct 24, 2022
thanks for the hint.
i tried to translate this to
@tanstack/solid-querybut ...the first fetch works, then result is in cache.
on the next page reload, after
persister.restoreClient, it fails to load result from cachei still dont see how QueryObserver is reading data from the cache
i.e. from
queryClient.clientState.queries... enough for todaydemo
edit:
clientStateis used inpersistQueryClientRestoretrace
query/packages/query-persist-client-core/src/persist.ts
Line 78 in 7741a2f
then
hydraterestores queries into thequeryCachequery/packages/query-core/src/hydration.ts
Line 124 in 7741a2f
case 1: query is not in queryCache -> add
query/packages/query-core/src/hydration.ts
Lines 153 to 154 in 7741a2f
queryCache.buildwill notify the query observers (this works for me)query/packages/query-core/src/queryCache.ts
Lines 118 to 119 in 7741a2f
case 2: query is in queryCache -> update
query/packages/query-core/src/hydration.ts
Line 148 in 7741a2f
query/packages/query-core/src/query.ts
Line 216 in 7741a2f
Query.dispatchwill notifyobserversandcachequery/packages/query-core/src/query.ts
Lines 574 to 578 in 7741a2f
observeris created *before* the cache is restoredquery/packages/react-query/src/useBaseQuery.ts
Line 75 in 99119a5
... but
observer.subscribeis called *after* the cache is restoredquery/packages/react-query/src/useBaseQuery.ts
Lines 86 to 88 in 99119a5