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
How It Works
Garbage Collection Time (gcTime)
The gcTime option determines how long inactive query data stays in memory. It’s critical for persistence:
Why does gcTime matter?
Why does gcTime matter?
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
gcTime to match or exceed maxAge.Cache Busting
Force cache invalidation when your app changes:buster string doesn’t match, the persisted data is discarded.
Automatic Removal
The persister automatically removes data that is:- Expired: Older than
maxAge - Busted:
busterstring doesn’t match - Error: Restoration threw an error
- Empty: No data found (
undefined)
API Reference
persistQueryClient
Restores cache immediately and subscribes to changes:
queryClient: The QueryClient to persistpersister: Persister implementationmaxAge: Maximum age in milliseconds (default: 24 hours)buster: String to force cache invalidationhydrateOptions: Options passed tohydrate()dehydrateOptions: Options passed todehydrate()
persistQueryClientSave
Manually save the cache:
persistQueryClientRestore
Manually restore the cache:
persistQueryClientSubscribe
Subscribe to cache changes without initial restore:
React Integration
PersistQueryClientProvider
For proper React integration, usePersistQueryClientProvider:
- Proper lifecycle management (subscribe/unsubscribe)
- Prevents race conditions during restoration
- Queries wait for restoration before fetching
- Integrates with React Suspense
client: QueryClient instancepersistOptions: Options forpersistQueryClientonSuccess: Called after successful restorationonError: 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
Date, Map, Set, and other non-JSON types.
Different Storage for Different Queries
Conditional Persistence
Best Practices
Match gcTime to maxAge
Match gcTime to maxAge
Always set
gcTime to at least your persist maxAge value:Use version for cache busting
Use version for cache busting
Invalidate cache on app updates:
Consider storage limits
Consider storage limits
localStorage has a ~5MB limit. For large caches, consider IndexedDB:
Use PersistQueryClientProvider in React
Use PersistQueryClientProvider in React
Prevents race conditions and integrates properly with React lifecycle:
Troubleshooting
Data not persisting
- Check that
gcTimeis high enough - Verify storage is working (check browser DevTools)
- Ensure
persistQueryClientis called after QueryClient creation - Check for quota exceeded errors
Data not restoring
- Check
maxAge- data may have expired - Verify
busterstring matches - Look for deserialization errors in console
- Ensure storage has data (check DevTools)
Performance issues
- Increase
throttleTimeto reduce save frequency - Use
shouldDehydrateQueryto persist less data - Consider IndexedDB for large datasets
- 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