I now that tRPC's caching mechanism is primarily handled by the React Query (now TanStack Query)
I would like to request more granular control over caching strategies within tRPC, similar to what is available in TanStack Query. Specifically, the ability to configure caching strategies such as "cache first," "network only," "stale while revalidate," and "cache and network" directly within tRPC hooks.
This feature is needed to provide developers with more control over how data is cached and retrieved, allowing for optimizations based on the specific needs of each query.
I would like to see additional options added to tRPC hooks (such as useQuery
and useMutation
) that allow for direct configuration of caching strategies. These options could mirror the options available in TanStack Query, providing a familiar interface for developers who have experience with that library.
Example usage might look like this:
Copy code
const { data, isLoading } = trpc.useQuery(['getUser', { id: '1' }], {
cacheStrategy: 'staleWhileRevalidate',
staleTime: 1000 * 60 * 5, // 5 minutes
});
In this example, the cacheStrategy option allows the developer to specify the desired caching strategy for the query.
An alternate solution would be to continue using tRPC as is, and manage caching strategies outside of tRPC by interacting directly with the TanStack Query client. However, this approach is less intuitive and requires additional boilerplate code, detracting from the simplicity and developer experience that tRPC provides.
Implementing this feature would bring the best of both worlds: the type-safety and simplicity of tRPC, combined with the powerful and flexible caching strategies of TanStack Query. It would also align tRPC more closely with industry standards for data fetching and caching in React applications.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too