Building Offline-First Experiences with React Query

- Published on
Offline-first design isn’t just for field workers anymore. Load shedding, travel, and network instability can hit any user, especially across emerging markets. React Query gives us the primitives to build resilient experiences without rewriting our apps from scratch.
Cache intentionally
React Query’s cache is the backbone of offline support. Configure staleTime
and cacheTime
to reflect how volatile each dataset is. For static reference data you can mark fetches as staleTime: Infinity
and prefetch them during onboarding so the experience feels instant.
const useArticles = () =>
useQuery(['articles'], fetchArticles, {
staleTime: 1000 * 60 * 5,
cacheTime: 1000 * 60 * 30,
retry: (failureCount, error) => shouldRetry(error) && failureCount < 2,
})
Mirror critical data to storage
Pair React Query with IndexedDB or Secure Storage so key data survives app restarts. The pattern I rely on:
- Fetch data with React Query.
- Persist the successful response via a query observer.
- On boot, hydrate the cache from storage using
queryClient.setQueryData
.
This gives you instant UI rendering while network requests happen in the background.
Design UX for graceful degradation
Offline-first UX isn’t about showing a banner that says “You’re offline.” It’s about enabling meaningful work:
- Allow composing content (messages, posts, forms) and queue it for sync.
- Provide progress indicators for pending mutations with the ability to retry or discard.
- Show audit trails so users know what actions will sync once connectivity returns.
Monitor for regressions
Offline features fail silently unless you test them. Add automated journeys that toggle the network stack via Playwright or Detox. In production, capture metrics on queued mutations, sync success rates, and the time windows users spend offline. Observability closes the loop.
The outcome
With a few patterns—hydrated cache, persisted storage, smart UX—you turn connectivity gaps into delightful experiences. Users stay in flow, support tickets drop, and your app becomes the tool they trust when it matters most.