Skip to content

Commit 49de132

Browse files
Fix IndexedDB cache comparison to ignore React Query metadata.
The previous hash comparison was including React Query metadata fields that change on every access (like dataUpdatedAt, dataUpdateCount, etc.). Now we extract and compare only the actual query data and errors, ignoring the constantly-changing metadata.
1 parent 4fc0296 commit 49de132

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/localStorage.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,32 @@ interface Persister {
736736
export function createIDBPersister(idbValidKey: string = STORAGE_KEYS.QUERY_CACHE): Persister {
737737
let lastPersistedHash: string | null = null;
738738

739+
// Extract only the actual query data, excluding metadata that changes on every access
740+
const extractQueryData = (client: any): any => {
741+
if (!client || !client.clientState) {
742+
return client;
743+
}
744+
745+
// Create a copy with only the actual data from queries
746+
const dataOnly = {
747+
queries: client.clientState.queries?.map((query: any) => ({
748+
queryKey: query.queryKey,
749+
queryHash: query.queryHash,
750+
data: query.state?.data,
751+
// Only include error if it exists
752+
...(query.state?.error ? {error: query.state.error} : {}),
753+
})),
754+
mutations: client.clientState.mutations?.map((mutation: any) => ({
755+
mutationKey: mutation.mutationKey,
756+
state: mutation.state?.status,
757+
data: mutation.state?.data,
758+
error: mutation.state?.error,
759+
})),
760+
};
761+
762+
return dataOnly;
763+
};
764+
739765
const hashObject = (obj: any): string => {
740766
const str = JSON.stringify(obj);
741767
let hash = 0;
@@ -750,8 +776,18 @@ export function createIDBPersister(idbValidKey: string = STORAGE_KEYS.QUERY_CACH
750776
const debouncedPersist = debounce(
751777
async (client: any) => {
752778
try {
753-
// Calculate hash of current state
754-
const currentHash = hashObject(client);
779+
// Extract only the data portion for comparison
780+
const dataOnly = extractQueryData(client);
781+
const currentHash = hashObject(dataOnly);
782+
783+
// Debug logging
784+
console.log('[IndexedDB] Hash comparison:', {
785+
current: currentHash,
786+
last: lastPersistedHash,
787+
equal: lastPersistedHash === currentHash,
788+
hasClientState: !!client?.clientState,
789+
queryCount: client?.clientState?.queries?.length || 0,
790+
});
755791

756792
// Skip persistence if state hasn't changed
757793
if (lastPersistedHash === currentHash) {

0 commit comments

Comments
 (0)