Skip to main content
The persistQueryClient utility provides a way to save your query cache to persistent storage (like localStorage, IndexedDB, or AsyncStorage) and restore it later. This is useful for:
  • Persisting data across page reloads
  • Offline-first applications
  • Reducing initial load times
  • Improving user experience with instant data

Installation

The persist client utilities are in separate packages:

Quick Start

With localStorage (Web)

With React Native AsyncStorage

Important: Set gcTime to at least the same value as maxAge (default 24 hours), or set it to Infinity to disable garbage collection.

How It Works

Garbage Collection Time (gcTime)

The gcTime option determines how long inactive query data stays in memory. It’s critical for persistence:
If gcTime is shorter than the persist maxAge, your cached data will be garbage collected before it’s supposed to expire. For example:
  • maxAge: 24 hours (persist keeps data for 24 hours)
  • gcTime: 5 minutes (default)
  • Result: Data is removed from memory after 5 minutes, even though persist expects it to last 24 hours
Set gcTime to match or exceed maxAge.

Cache Busting

Force cache invalidation when your app changes:
When the cache is restored, if the buster string doesn’t match, the persisted data is discarded.

Automatic Removal

The persister automatically removes data that is:
  1. Expired: Older than maxAge
  2. Busted: buster string doesn’t match
  3. Error: Restoration threw an error
  4. Empty: No data found (undefined)

API Reference

persistQueryClient

Restores cache immediately and subscribes to changes:
Options:
  • queryClient: The QueryClient to persist
  • persister: Persister implementation
  • maxAge: Maximum age in milliseconds (default: 24 hours)
  • buster: String to force cache invalidation
  • hydrateOptions: Options passed to hydrate()
  • dehydrateOptions: Options passed to dehydrate()

persistQueryClientSave

Manually save the cache:
Throttled to once per second by default.

persistQueryClientRestore

Manually restore the cache:

persistQueryClientSubscribe

Subscribe to cache changes without initial restore:

React Integration

PersistQueryClientProvider

For proper React integration, use PersistQueryClientProvider:
Benefits:
  • Proper lifecycle management (subscribe/unsubscribe)
  • Prevents race conditions during restoration
  • Queries wait for restoration before fetching
  • Integrates with React Suspense
Props:
  • client: QueryClient instance
  • persistOptions: Options for persistQueryClient
  • onSuccess: Called after successful restoration
  • onError: Called if restoration fails

useIsRestoring

Check if restoration is in progress:

Built-in Persisters

Sync Storage Persister

For synchronous storage (localStorage, sessionStorage):

Async Storage Persister

For asynchronous storage (AsyncStorage, IndexedDB):

Custom Persister

Create your own persister for any storage backend:

Persister Interface

Advanced Examples

Persisting Specific Queries Only

Custom Serialization

This allows you to persist Date, Map, Set, and other non-JSON types.

Different Storage for Different Queries

Conditional Persistence

Best Practices

Always set gcTime to at least your persist maxAge value:
Invalidate cache on app updates:
localStorage has a ~5MB limit. For large caches, consider IndexedDB:
Prevents race conditions and integrates properly with React lifecycle:

Troubleshooting

Data not persisting

  1. Check that gcTime is high enough
  2. Verify storage is working (check browser DevTools)
  3. Ensure persistQueryClient is called after QueryClient creation
  4. Check for quota exceeded errors

Data not restoring

  1. Check maxAge - data may have expired
  2. Verify buster string matches
  3. Look for deserialization errors in console
  4. Ensure storage has data (check DevTools)

Performance issues

  1. Increase throttleTime to reduce save frequency
  2. Use shouldDehydrateQuery to persist less data
  3. Consider IndexedDB for large datasets
  4. Implement custom serialization for better performance

Further Reading

Sync Storage Persister

Learn about the synchronous storage persister

Async Storage Persister

Learn about the asynchronous storage persister

Hydration

Learn about dehydration and hydration

Offline Queries

Understand network modes and offline behavior