From 620e3b4e9d463f6a01308329d531e069a17db4f4 Mon Sep 17 00:00:00 2001 From: Nicholas Firth-McCoy Date: Wed, 8 Mar 2023 10:12:40 +1100 Subject: [PATCH 1/2] fix: Relax undefined check against storage to handle null values Fixes #4957. On Android WebView if `setDomStorageEnabled` is not configured, `window.localStorage` will be `null` (rather than `undefined`). Relax the existing conditional to check if `storage` is present to handle both `null` and `undefined` values to remove a gotcha here. --- packages/query-async-storage-persister/src/index.ts | 7 ++++--- packages/query-sync-storage-persister/src/index.ts | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/query-async-storage-persister/src/index.ts b/packages/query-async-storage-persister/src/index.ts index 52361028f4..bc92f11419 100644 --- a/packages/query-async-storage-persister/src/index.ts +++ b/packages/query-async-storage-persister/src/index.ts @@ -19,9 +19,10 @@ export type AsyncPersistRetryer = (props: { interface CreateAsyncStoragePersisterOptions { /** The storage client used for setting and retrieving items from cache. - * For SSR pass in `undefined`. + * For SSR pass in `undefined`. Note that window.localStorage can be + * `null` in Android WebViews depending on how they are configured. */ - storage: AsyncStorage | undefined + storage: AsyncStorage | undefined | null /** The key to use when storing the cache */ key?: string /** To avoid spamming, @@ -49,7 +50,7 @@ export const createAsyncStoragePersister = ({ deserialize = JSON.parse, retry, }: CreateAsyncStoragePersisterOptions): Persister => { - if (typeof storage !== 'undefined') { + if (storage) { const trySave = async ( persistedClient: PersistedClient, ): Promise => { diff --git a/packages/query-sync-storage-persister/src/index.ts b/packages/query-sync-storage-persister/src/index.ts index 3d6bf82df8..095b50480f 100644 --- a/packages/query-sync-storage-persister/src/index.ts +++ b/packages/query-sync-storage-persister/src/index.ts @@ -12,9 +12,10 @@ interface Storage { interface CreateSyncStoragePersisterOptions { /** The storage client used for setting and retrieving items from cache. - * For SSR pass in `undefined`. + * For SSR pass in `undefined`. Note that window.localStorage can be + * `null` in Android WebViews depending on how they are configured. */ - storage: Storage | undefined + storage: Storage | undefined | null /** The key to use when storing the cache */ key?: string /** To avoid spamming, @@ -42,7 +43,7 @@ export function createSyncStoragePersister({ deserialize = JSON.parse, retry, }: CreateSyncStoragePersisterOptions): Persister { - if (typeof storage !== 'undefined') { + if (storage) { const trySave = (persistedClient: PersistedClient): Error | undefined => { try { storage.setItem(key, serialize(persistedClient)) From 89dc2b21006443c30fe81e939064d1b185c347c8 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Fri, 17 Mar 2023 17:41:25 +0100 Subject: [PATCH 2/2] docs: update storage type --- docs/react/plugins/createAsyncStoragePersister.md | 2 +- docs/react/plugins/createSyncStoragePersister.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/react/plugins/createAsyncStoragePersister.md b/docs/react/plugins/createAsyncStoragePersister.md index f19cba9e83..48bcd9cf44 100644 --- a/docs/react/plugins/createAsyncStoragePersister.md +++ b/docs/react/plugins/createAsyncStoragePersister.md @@ -74,7 +74,7 @@ createAsyncStoragePersister(options: CreateAsyncStoragePersisterOptions) ```tsx interface CreateAsyncStoragePersisterOptions { /** The storage client used for setting an retrieving items from cache */ - storage: AsyncStorage + storage: AsyncStorage | undefined | null /** The key to use when storing the cache to localStorage */ key?: string /** To avoid localStorage spamming, diff --git a/docs/react/plugins/createSyncStoragePersister.md b/docs/react/plugins/createSyncStoragePersister.md index 46c28f1bb5..b713c6693b 100644 --- a/docs/react/plugins/createSyncStoragePersister.md +++ b/docs/react/plugins/createSyncStoragePersister.md @@ -88,7 +88,7 @@ createSyncStoragePersister(options: CreateSyncStoragePersisterOptions) ```tsx interface CreateSyncStoragePersisterOptions { /** The storage client used for setting an retrieving items from cache (window.localStorage or window.sessionStorage) */ - storage: Storage + storage: Storage | undefined | null /** The key to use when storing the cache */ key?: string /** To avoid spamming,