Skip to content
Source
Sign inSign up
TanStack/query

query-sync-storage-persister raises on Android WebView if `setDomStorageEnabled` is not configured #4957

Closednfm(GitHub profile, opens in a new tab) opened this issue 2 comments

Describe the bug

An Android WebView that is not configured with settings.setDomStorageEnabled(true) will not expose window.localStorage for use.

query-sync-storage-persister guards against the value for storage being undefined:

https://github.com/TanStack/query/blob/b82f05e3028718f03eb132a4dd92dc3d7623f7b5/packages/query-sync-storage-persister/src/index.ts#L45

I think this is necessary for old browsers that predate local storage, where window.localStorage === undefined. Given the persister is typically initialized with createSyncStoragePersister({ storage: window.localStorage }) that makes sense.

However, in the Android WebView case, it appears that window.localStorage === null - the property is present on window but the value is null rather than undefined.

As a result, the conditional linked above passes (typeof null is "object" 🤷 ), and removeClient can later raise with TypeError: Cannot read properties of null (reading 'removeItem'):

https://github.com/TanStack/query/blob/b82f05e3028718f03eb132a4dd92dc3d7623f7b5/packages/query-sync-storage-persister/src/index.ts#L81-L83

It looks like persistClient doesn't hit this because trySave has some error handling around it:

https://github.com/TanStack/query/blob/b82f05e3028718f03eb132a4dd92dc3d7623f7b5/packages/query-sync-storage-persister/src/index.ts#L46-L53

I can guard against this when initializing createSyncStoragePersister, but I think it probably makes sense to relax the conditional in createSyncStoragePersister to just if (storage) { ... } unless there's a good reason not to.

Your minimal, reproducible example

None sorry, but the bug is fairly trivial

Steps to reproduce

To reproduce you'd need an app installed with a WebView configured in a particular way (unsure if it would need to be configured as settings.setDomStorageEnabled(false), or if that's the default).

Whatever triggers removeClient would then throw rather than noop.

Expected behavior

If window.localStorage is null, initializing the query persister via createSyncStoragePersister({ storage: window.localStorage }) should behave the same as if window.localStorage is undefined - queries aren't persisted but no exceptions are raised.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Android
  • Browser: Chrome Mobile WebView
  • Version: Appears to affect all versions (have seen Sentry reports from Chrome Mobile WebView 72.0.3626 all the way through to Chrome Mobile WebView 110.0.5481)

TanStack Query version

@tanstack/query-sync-storage-persister@4.24.4

TypeScript version

4.7.4

Additional context

No response

  1. I think this is necessary for old browsers that predate local storage, where window.localStorage === undefined.

    not quite. we guard against undefined because we allow to explicitly pass undefined in for cases where you server-render you application with local persistence. On the server, there is no storage, but you still have to render the app:

    https://github.com/TanStack/query/blob/b82f05e3028718f03eb132a4dd92dc3d7623f7b5/packages/query-sync-storage-persister/src/index.ts#L14-L17

    But yeah, we can relax the check, because the typings don't allow anything else anyways. Would you like to contribute that?

  2. TkDodo added labelspackage: sync-storage-persister

  3. Thanks @TkDodo, and sorry for the delayed response on my end!

    I've just opened a PR for this: https://github.com/TanStack/query/pull/5095. First time contributing to TanStack/query - please let me know if there's anything I need to adjust.

  4. TkDodo closed this

Sign in to join this conversation.