Skip to content

Commit

Permalink
fix(query-sync-storage-persister): Relax undefined check against stor…
Browse files Browse the repository at this point in the history
…age 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.
  • Loading branch information
nfm committed Mar 7, 2023
1 parent aa2f944 commit f6f21b6
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/query-sync-storage-persister/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit f6f21b6

Please sign in to comment.