invalidation - useInfiniteQuery with local-first cache
hi,
i've got a useInfiniteQuery which is browsing collections using a local-first (SQLite/IndexedDB) cache of a remote collection
sometimes i want to invalidate the query and have it refetch all pages, but only from the local store
other times i want to do an SWR-type refresh, and trigger a fetch/refresh of data from the API, followed by a local-only refetch once the refreshed data is integrated into the local cache
so i think i would like to be able to tell
invalidateQueries or fetchNextPage/fetchPreviousPage whether i want to do a local-only operation or hit the API
but currently it seems that neither invalidateQueries nor fetchNextPage / fetchPreviousPage accept any values which will eventually be passed to the query-fn to allow the query-fn to make such a decision about whether to hit the API or not
am i missing any other way to achieve this ?7 Replies
rare-sapphireOP•4y ago
i currently have a working solution using a timeout, but it's less than ideal... the app often knows when it wants a local-only or API refetch
xenial-black•4y ago
there's a "workaround" at best:
the
meta field is passed to the queryFn. if you make the meta field a ref and read ref.current inside it, you can write to the ref before invalidation.
but generally i would say: if you want to force to read from the real remote - clear the value from the indexDB, then invalidate. That way, there is nothing to read from and it has to go to the remote api ?rare-sapphireOP•4y ago
thanks for the quick response!
clearing the local-store would obvs work ... but would stop the app working well when offline - and maximal offline operation is one of our primary goals
i'll investigate the meta option further... although i suspect i'll be stuck with the timeout i currently have
xenial-black•4y ago
You could only clear the local storage when you're online. Check the onlineManager from RQ
rare-sapphireOP•4y ago
hmm... race conditions aside, that's still going to mess up our offline operation - some of the queries are for the "start of the collection" - to get that to return nothing i would have to clear the entire cache, which can easily be many thousands of records (most perfectly valid and all fine for stale presentation), and which we don't want to refetch in one hit, but if we leave fetching to later then the user may be offline later
i'll think on it further - but would you be open to a PR to allow something to be passed from
invalidateQueries/fetchNextPage/fetchPreviousPage to the query fn ?xenial-black•4y ago
oh, so there's no way to clear a single entry from the offline cache?
would you be open to a PR to allow something to be passed from invalidateQueries/fetchNextPage/fetchPreviousPage to the query fn ?we could potentially allow
meta to be passed in
but meta is persisted on the query, so then the query will have this meta and all other fetches will also "get it " ....rare-sapphireOP•4y ago
a single item can be cleared from the cache, but if a "start of collection" query is then issued it will still get a list of items, until all items in the collection have been cleared from the cache
hmm. maybe i should remove the triggering of API requests from the
useInfiniteQuery query-fn entirely, and do it elsewhere... leaving the query-fn just browsing collections from the local-store 🤔
having thought it through, i don't think meta works for me - because all fetches will get it