Skip to content

Commit b3e9dd2

Browse files
fix(query-persist-client-core/createPersister): await 'storage.setItem' when persisting a query
`persistQuery` in packages/query-persist-client-core/src/createPersister.ts awaited `serialize` but not `storage.setItem`, so the promise it returns resolved as soon as the write was started rather than when it finished. Two consequences with an asynchronous storage such as React Native AsyncStorage or idb-keyval. `await persister.persistQueryByKey(queryKey, queryClient)`, which the createPersister docs show being used in `onMutate` to persist an optimistic update, returned before the entry was in storage. And a failing write, for example a quota error, surfaced as an unhandled promise rejection instead of rejecting the returned promise. `createAsyncStoragePersister` already awaits its `storage.setItem` call in packages/query-async-storage-persister/src/index.ts. Adding the `await` fixes both. The two new tests in packages/query-persist-client-core/src/__tests__/createPersister.test.ts cover the ordering and the rejection, and both fail without the change.
1 parent 1893a96 commit b3e9dd2

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-persist-client-core': patch
3+
---
4+
5+
fix(query-persist-client-core/createPersister): await the storage write in `persistQuery`, so `persistQueryByKey` resolves only once the entry has actually been written and rejects when the write fails instead of leaving an unhandled rejection

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,64 @@ describe('createPersister', () => {
521521
},
522522
})
523523
})
524+
525+
it('should not resolve until an asynchronous write has finished', async () => {
526+
const storage = getFreshStorage()
527+
let releaseWrite!: () => void
528+
const writeGate = new Promise<void>((resolve) => {
529+
releaseWrite = resolve
530+
})
531+
const { persister, client, queryKey, storageKey } = setupPersister(
532+
['foo'],
533+
{
534+
storage: {
535+
...storage,
536+
setItem: async (key, value) => {
537+
await writeGate
538+
await storage.setItem(key, value)
539+
},
540+
},
541+
},
542+
)
543+
544+
client.setQueryData(queryKey, 'baz')
545+
546+
let settled = false
547+
const persisted = persister
548+
.persistQueryByKey(queryKey, client)
549+
.then(() => {
550+
settled = true
551+
})
552+
553+
await vi.advanceTimersByTimeAsync(0)
554+
555+
expect(settled).toBe(false)
556+
expect(await storage.getItem(storageKey)).toBeUndefined()
557+
558+
releaseWrite()
559+
await persisted
560+
561+
expect(settled).toBe(true)
562+
expect(JSON.parse(await storage.getItem(storageKey))).toMatchObject({
563+
state: { data: 'baz' },
564+
})
565+
})
566+
567+
it('should reject when the write fails', async () => {
568+
const storage = getFreshStorage()
569+
const { persister, client, queryKey } = setupPersister(['foo'], {
570+
storage: {
571+
...storage,
572+
setItem: () => Promise.reject(new Error('storage is full')),
573+
},
574+
})
575+
576+
client.setQueryData(queryKey, 'baz')
577+
578+
await expect(
579+
persister.persistQueryByKey(queryKey, client),
580+
).rejects.toThrow('storage is full')
581+
})
524582
})
525583

526584
describe('retrieveQuery', () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export function experimental_createQueryPersister<TStorageValue = string>({
188188
async function persistQuery(query: Query) {
189189
if (storage != null) {
190190
const storageKey = `${prefix}-${query.queryHash}`
191-
storage.setItem(
191+
await storage.setItem(
192192
storageKey,
193193
await serialize({
194194
state: query.state,

0 commit comments

Comments
 (0)