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 (#5095)

* 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.

* docs: update storage type

---------

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
  • Loading branch information
nfm and TkDodo authored Mar 17, 2023
1 parent 73e21ce commit d23d893
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/react/plugins/createAsyncStoragePersister.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion docs/react/plugins/createSyncStoragePersister.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions packages/query-async-storage-persister/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Error | undefined> => {
Expand Down
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 d23d893

Please sign in to comment.