Skip to content

Commit 03cb202

Browse files
committed
fix: restore undefined persister query payloads
1 parent c0b26ca commit 03cb202

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

packages/query-persist-client-core/src/__tests__/createPersister.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,26 @@ describe('createPersister', () => {
309309
expect(query.fetch).toHaveBeenCalledTimes(0)
310310
})
311311

312+
it('should restore persisted undefined data from storage', async () => {
313+
const storage = getFreshStorage()
314+
const { context, persister, query, queryFn } = setupPersister(['foo'], {
315+
storage,
316+
refetchOnRestore: false,
317+
})
318+
319+
query.setData(undefined)
320+
await persister.persistQuery(query)
321+
322+
query.fetch = vi.fn()
323+
await persister.persisterFn(queryFn, context, query)
324+
325+
await vi.advanceTimersByTimeAsync(0)
326+
327+
expect(queryFn).toHaveBeenCalledTimes(0)
328+
expect(query.fetch).toHaveBeenCalledTimes(0)
329+
expect(query.state.data).toBe(undefined)
330+
})
331+
312332
it('should store item after successful fetch', async () => {
313333
const storage = getFreshStorage()
314334
const {

packages/query-persist-client-core/src/createPersister.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import type {
1313
QueryState,
1414
} from '@tanstack/query-core'
1515

16+
type PersistedResult<T> = {
17+
data: T
18+
}
19+
1620
export interface PersistedQuery {
1721
buster: string
1822
queryHash: string
@@ -125,7 +129,7 @@ export function experimental_createQueryPersister<TStorageValue = string>({
125129
async function retrieveQuery<T>(
126130
queryHash: string,
127131
afterRestoreMacroTask?: (persistedQuery: PersistedQuery) => void,
128-
) {
132+
): Promise<PersistedResult<T> | undefined> {
129133
if (storage != null) {
130134
const storageKey = `${prefix}-${queryHash}`
131135
try {
@@ -149,7 +153,7 @@ export function experimental_createQueryPersister<TStorageValue = string>({
149153
)
150154
}
151155
// We must resolve the promise here, as otherwise we will have `loading` state in the app until `queryFn` resolves
152-
return persistedQuery.state.data as T
156+
return { data: persistedQuery.state.data as T }
153157
}
154158
}
155159
} catch (err) {
@@ -209,7 +213,7 @@ export function experimental_createQueryPersister<TStorageValue = string>({
209213

210214
// Try to restore only if we do not have any data in the cache and we have persister defined
211215
if (matchesFilter && query.state.data === undefined && storage != null) {
212-
const restoredData = await retrieveQuery(
216+
const restoredData = await retrieveQuery<T>(
213217
query.queryHash,
214218
(persistedQuery: PersistedQuery) => {
215219
// Set proper updatedAt, since resolving in the first pass overrides those values
@@ -228,7 +232,7 @@ export function experimental_createQueryPersister<TStorageValue = string>({
228232
)
229233

230234
if (restoredData !== undefined) {
231-
return Promise.resolve(restoredData as T)
235+
return Promise.resolve(restoredData.data)
232236
}
233237
}
234238

0 commit comments

Comments
 (0)