-
Documentation Improvement: Warning about Promise serialization issues in IndexedDB persistenceDescriptionThe current documentation for This issue commonly occurs on initial application loads in new environments (like incognito mode) where all queries start in a loading state with no existing cache. The current documentation example doesn't address this scenario, potentially leading developers to spend significant time debugging these errors. Current Documentation IssueIn the persistQueryClient documentation, the IndexedDB example doesn't include any handling for queries in a loading state or containing Promise objects: import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'
import { QueryClient } from '@tanstack/react-query'
import { persistQueryClient } from '@tanstack/react-query-persist-client'
import { get, set, del } from 'idb-keyval'
const queryClient = new QueryClient({
defaultOptions: {
queries: {
cacheTime: 1000 * 60 * 60 * 24, // 24 hours
},
},
})
const persister = createSyncStoragePersister({
storage: {
getItem: async (key) => {
const value = await get(key)
return value
},
setItem: async (key, value) => {
await set(key, value)
},
removeItem: async (key) => {
await del(key)
},
},
})
persistQueryClient({
queryClient,
persister,
}) Suggested Improvements
Proposed Documentation Addition// New section to add after the basic example:
/*
Important Note: IndexedDB cannot serialize Promise objects that exist in loading queries.
This commonly causes DataCloneError exceptions on initial application loads when all queries
start in a loading state. To prevent this, use the shouldDehydrateQuery option to filter out
loading queries before persistence.
*/
// Enhanced example:
persistQueryClient({
queryClient,
persister,
dehydrateOptions: {
shouldDehydrateQuery: (query) => {
// Only persist queries that have completed loading and don't have errors
return query.state.status !== 'loading' && !query.state.error;
},
},
// Add error handling for serialization failures
onError: (error) => {
console.error('Error persisting query cache:', error);
}
}) Additional RecommendationsConsider adding a section on best practices for persistence that includes:
ImpactThis improvement would help developers avoid a common pitfall when implementing persistence with IndexedDB, reducing debugging time and improving the developer experience with Tanstack Query. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
query/packages/query-core/src/hydration.ts Lines 108 to 110 in 6ca0eb7 |
Beta Was this translation helpful? Give feedback.
you overwrite the default behaviour, you need: