T
tRPC

❓-help

Cache not working for `useQuery`

JJokcy2/27/2023
I have a query like this:
const { data: article, isFetching } = api.public.getArticle.useQuery({
id: targetId,
});
const { data: article, isFetching } = api.public.getArticle.useQuery({
id: targetId,
});
which targetId is a dynamic value. When I change targetId from a to b then switch back from b to a, the useQuery hook still send a new request for a instead of using cache. Any idea how to make the cache work?
Nnlucas2/27/2023
You can set the stale time and cache time for queries and globally. It’s quite aggressive by default I believe https://tanstack.com/query/v4/docs/react/guides/important-defaults It’s probably working as intended though
JJokcy2/28/2023
Actually I did, I set cacheTime: Infinity it still not work. Note useQuery should have default cacheTime which is 5m so my code should work with cache by default.
Nnlucas2/28/2023
And stale time?
JJokcy2/28/2023
Yeah, sorry did not notic the stale time, work now THX.

Looking for more? Join the community!

Recommended Posts
Zod File Upload Validation with Open-Api Support?Hi guys, anyone know how to validate file upload with zod and get also open-api support?Zod validation and open-api support for File on the server?Hi guys, anyone know how to validate a File upload using zod? and also have open-api support?is context cached?If I put an object on the context that represents the User record from my database... ``` export asyJSON inferred router output not matchingHello. I have a procedure query which is returning Json from a postgresql db using Prisma. The type Best way to implement input based validation on a router procedureHi guys, bit of a noob. I have already created a 'protectedProcedure', ensuring the user is logged [Help] Turbo shared typesI have a turborepo with two apps (web and embed). web is a t3 stack and embed is a create-react-app.Cache SSG helper responseI'm using `createProxySSGHelpers` in GSSP with `ssr: false` in the global config. I trying to cacheInput is too big for a single dispatchI decided to try tRPC for my Crypto analytics dashboard. However, I'm having a hard time passing thetypesafe permissionsHi, So I wanted to infer all the procedures from my router recursively & assign a permission (stringawaiting for procedure & logging the response.Hi, I was wondering if there is a way to handle the return object via the post-middleware's? I know createCaller Dependency Injection in Middleware ctx ?`createCaller` makes it really easy to inject dependencies via anything that's created during the `cbest practices for organizing routes/procedures?i'm trying to find some practices/styles in which people generally define routes with trpc. currentl