1

I have simple web app with firestore, that has one onSnapshot listener. I have enabled persistence and always check if data from the listener comes from server or local cache. Everything works as expected (fromCache metadata is always true, if there were no changes to documents) when I have my application opened in one browser tab. But when I call onSnapshot from multiple tabs, fromCache metadata is always false in all tabs.

I always call firebaseApp.firestore().enablePersistence({synchronizeTabs: true}) right after firebase.initializeApp()

With synchronizeTabs: true I would expect all tabs to get data from local cache, but this expected behaviour returns only after closing all other tabs except one (doesn't matter which one)

What's the reason to synchronize persistence between browser tabs, but get the data from server anyway? Or is it just confusing metadata?

I use chrome 91.0.4472.124 and "firebase": "^8.7.1"

Flodgar
  • 123
  • 1
  • 5

1 Answers1

2

onSnapshot fetches the initial source from cache then the server, as all changes need to be evaluated from that point forwards. The unfortunate side effect however is that all listeners are unique from each other as they register the actions with the backend which will trigger each onSnapshot when future updates occur.

You aren't seeing any enhancement with Persistence, as its implementation is about synchronizing cached data between tabs for direct reads/writes, not sharing onSnapshot listeners.

DIGI Byte
  • 4,225
  • 1
  • 12
  • 20