question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failing to enable caching with `useQueries`

See original GitHub issue

As the title indicates, I’m having trouble seeing any effects of the cache with the useQueries function. Will include the code I’m testing below. While the query itself works, properly running fetchBond and returning correct results, both localstorage and cache in my browser remain empty, and a refresh of the page prompts the loading of new data (no stale data appears, I get a full loading state)

I’m hoping to write a web3 query with a composable like this:

export const useBondListQuery = (
  bondIds: number[] | Ref<number[]>,
  options?: Omit<
    UseQueryOptions<unknown, unknown, unknown, (string | number)[]>,
    "queryFn" | "queryKey"
  >
) => {
  const { status } = useWeb3();
  const { contracts } = useContracts();

  const queryKeys = computed(() =>
    ((bondIds as Ref<number[]>).value || bondIds).map((bondId) => ({
      queryKey: ["bond", bondId],
      queryFn: (ctx: QueryFunctionContext) =>
        fetchBond(contracts.value.Core, ctx),
      ...queryOptions,
      ...options,
      enabled:
        bondId !== undefined &&
        status.value === ConnectionStatus.CONNECTED &&
        contracts.value.Core !== undefined &&
        (options?.enabled ?? true),
    }))
  );

  return useQueries(queryKeys) as readonly QueryObserverResult<
    FetchBondResult,
    unknown
  >[];
};

I’ve changed some cache settings in an attempt to fix this. The queryOptions you see referenced is containing these. As you can see, I’m hoping to minimize queries and store a lot of this data on browser cache if possible (it’s mostly static data tbh).

const queryOptions = {
  refetchOnWindowFocus: false,
  cacheTime: 1000 * 60 * 5,
  staleTime: 1000 * 60 * 5,
  retry: false,
};

Let me know if this is possible or if I’m missing something from the documentation entirely

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
DamianOsipiukcommented, Nov 21, 2022
0reactions
chetzofcommented, Nov 21, 2022

I think the keyword that you use here is sessions.

Vue-Query does not store the data between sessions by default.

Vue-Query caches the data between multiple calls to the same endpoint in one session. Ex multiple components request same data, or you navitage to a different page in the app and back.

To make data persist between multiple sessions you would have to use persist plugin. Unfortunately it’s not yet ported to Vue-Query. I intend to add that capability after migration to @TanStack monorepo.

Is see that the package was migrated to @Tanstack monorepo. Is is possible to use the persist plugin already?

Read more comments on GitHub >

github_iconTop Results From Across the Web

useQuery fails to return cached data when variables ... - GitHub
Using a query that get data based on tab value works the first time but switching back it will keep the wrong cached...
Read more >
React Query - query is not using cache? - Stack Overflow
React Query will cache the data of the query by default, but that does not affect whether or not it thinks that data...
Read more >
Advanced topics on caching in Apollo Client
Advanced topics on caching in Apollo Client. This article describes special cases and considerations when using the Apollo Client cache. Bypassing the cache....
Read more >
useQuery | TanStack Query Docs
When different cache times are specified, the longest one will be used. If set to Infinity , will disable garbage collection.
Read more >
Fetch, Cache, and Update Data Effortlessly with React Query
You can also configure the query client but we skip it for now and go with the defaults. Now, let's fetch some queries!...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found