feat: first-class offline persistence helpers for tRPC React Query integrations
Describe the feature you'd like to request
I’d like first-class offline persistence support for the tRPC React Query integrations ( trpc/react-query and/or trpc/tanstack-react-query ).
Right now, getting a robust offline-first experience with tRPC + React Query / TanStack Query requires a fair amount of manual wiring: setting up a persister, integrating with onlineManager, and configuring mutation defaults so paused mutations are persisted and replayed correctly. All the building blocks exist, but they’re hidden in tests and examples rather than exposed as a clear, supported feature.
A small, well-designed set of helpers that make “offline + persistence + replay” a first-class capability would make it much easier to build tRPC apps that tolerate flaky or offline networks.
Describe the solution you'd like to see
I’d love to see built-in helpers in the React Query integrations that:
Wrap the existing client / query utils setup so that: A QueryClient is configured with a persister (e.g. localStorage, AsyncStorage, or a custom storage interface). onlineManager (or equivalent) is integrated so that the app has a canonical notion of online/offline state. Mutation defaults are configured such that: Mutations triggered while offline are paused and persisted. When the app restarts and comes back online, those mutations are automatically resumed with the expected behavior. The exact API shape is open, but something along these lines would be very helpful (just as an illustrative example, not a prescription):
ts const { trpc, queryClient, persister } = createOfflineTRPCClient({ // router types, // storage (localStorage / AsyncStorage / custom), // optional persistence + retry options, }); Under the hood, this would reuse the patterns demonstrated in the existing offline tests (using createSyncStoragePersister, onlineManager, and createTRPCQueryUtils), but expose them as a documented, reusable API rather than requiring users to re-implement that pattern manually.
Ideally, the design would:
Work for both trpc/react-query and trpc/tanstack-react-query (or make it very clear if the first iteration targets one of them). Be flexible about storage (sync vs async) and retry behavior, while still giving a “batteries included” default.
Describe alternate solutions
Current alternatives are:
Reimplement the test pattern in userland: Manually configure persistQueryClient / createSyncStoragePersister. Manage onlineManager integration. Hand-roll mutation defaults so paused offline mutations are persisted, then resumed on reconnect. This works, but is easy to get subtly wrong, and the knowledge is mostly encoded in tests rather than in public APIs or docs. Rely solely on TanStack Query persistence docs and bolt tRPC on top: Possible, but you lose out on the higher-level DX that tRPC already provides around React Query usage. There’s no “tRPC-native” story for offline persistence today; everyone reinvents a slightly different wheel. A first-party tRPC helper would give a canonical, documented approach that’s designed specifically for the tRPC integrations, avoids copy-paste from tests, and reduces fragmentation across the community.
Additional information
There is already prior art in this repo: packages/react-query/test/offline.test.tsx and related helpers show how to build a correct offline + persistence setup today. There are also community questions / discussions about how to wire tRPC with React Query persistence (e.g. discussions around persistQueryClient and offline behavior), which suggests user demand for a “blessed” pattern. This feature feels like a natural extension of tRPC’s existing React Query integrations: it doesn’t introduce new concepts, but packages them into a more approachable DX for offline-first apps.
Contributing
- ♂️ Yes, I'd be down to file a PR implementing this feature!
Source: trpc/trpc