Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: using multi-tab persistence, the queries in the secondary tabs don't report metadata.fromCache=false #8314

Comments

@atsjo
Copy link

atsjo commented Jun 13, 2024

Operating System

Latest Windows 10/11 (probably all os)

Browser Version

Latest Chrome/Edge (probably all browsers)

Firebase SDK Version

10.12.2

Firebase SDK Product:

Firestore

Describe your project's tooling

Angular 18 app via newest tooling

Describe the problem

When using multi-tab persistence and realtime listeners via onSnapshot and includeMetadataChanges: true, the queries in secondary tabs never report metadata.fromCache=false, so I cannot detect correct online state for queries when having multiple tabs...

This works fine with 10.12.0, and I tracked down a PR included in 10.12.1 related to a multi-tab issue (#8247) which probably causes this issue. This PR says it fixes that queries in secondary tabs include empty snapshots, but it SHOULD report empty snapshots with changed metadata when includeMetadataChanges=true, or else we cannot determine online state of queries...

Steps and code to reproduce issue

  1. create app with multi-tab persistence that opens a realtime listener via onSnapshot and with includeMetadataChanges: true (initializeFirestore(getApp(), { localCache: persistentLocalCache({ tabManager: persistentMultipleTabManager() }) });)
  2. Open app in multiple tabs in browser, and observe the the queries in the secondary tabs never report metadata.fromCache=false
@atsjo atsjo added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Jun 13, 2024
@atsjo
Copy link
Author

atsjo commented Jun 13, 2024

This is NOT a question, but a serious regression introduced in 10.12.1 related to multi-tab persistence...

@atsjo atsjo changed the title using multi-tab persistence, the queries in the secondary tabs don't report metadata.fromCache=false BUG: using multi-tab persistence, the queries in the secondary tabs don't report metadata.fromCache=false Jun 13, 2024
@milaGGL milaGGL self-assigned this Jun 13, 2024
@jbalidiong jbalidiong added needs-attention and removed new A new issue that hasn't be categoirzed as question, bug or feature request labels Jun 13, 2024
@milaGGL
Copy link
Contributor

milaGGL commented Jun 13, 2024

Hi @atsjo, thank you for reporting this issue.

I cannot detect correct online state for queries when having multiple tabs

Are you trying to switch network condition between online and offline, and expecting to see snapshots with "fromCache: true/false"? I have tried in local test app, and it is working as expected.

@atsjo
Copy link
Author

atsjo commented Jun 13, 2024

No, but my queries always fetch newer than what's in my local cache, so they usually return 0 elements, with fromCache true, but then return 0 again with fromCache false, to signal data is up to date with backend

@milaGGL
Copy link
Contributor

milaGGL commented Jun 13, 2024

Could you please provide reproduction steps, and what was expected vs what is the current behaviour?

@atsjo
Copy link
Author

atsjo commented Jun 14, 2024

I'll try to make a repro... I expect to always receive fromCache=false so I know I'm up to date with the server, and I use this to detect if I'm offline or online. This doesn't work in 10.12.2, but DOES work in 10.12.0.

@atsjo
Copy link
Author

atsjo commented Jun 17, 2024

Hi @milaGGL , I've made a repro and deployed it with different versions of the sdk to different sites:
v10.12.2 -> https://test-57faf.web.app/ (NOT working with multiple tabs)
v10.12.0 -> https://test1-57faf.web.app/ (working with multiple tabs)

You can just use view source to see the source for these projects, as no bundling is used, and all logic is contained within a couple of lines in index.html. Open the first link in 2 tabs (preferably in separate windows so you see both), and refresh the tabs in turns, and you should see one or both turning red. It takes some tries the first time to trigger this, but I have verified in Chrome/Edge and Firefox on windows, AND on my pixel phone with chrome... This does NOT happen with link nr 2.... I use the fromCache metadata to determine when I'm up to date and connected to the firestore backend, as this is the preferred way of doing that according to the docs.... In my app I open 3 snapshot listeners on startup, and if people have multiple tabs or choose to install it as a pwa, this error is triggered...

The firestore db used here allows only reads, and contain no data.... Code used in repro listed below:

<body>
  <div id="info">fromCache</div>
  <script type="module">
    import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.x/firebase-app.js";
    import { initializeFirestore, persistentLocalCache, persistentMultipleTabManager, onSnapshot, collection } from "https://www.gstatic.com/firebasejs/10.12.x/firebase-firestore.js";
  
    const firebaseConfig = ...
  
    const app = initializeApp(firebaseConfig);
    const fs = initializeFirestore(app, { localCache: persistentLocalCache({ tabManager: persistentMultipleTabManager() }) });

    onSnapshot(collection(fs, 'test'), { includeMetadataChanges: true }, { next: snap => {
      document.getElementById('info').style.backgroundColor = snap.metadata.fromCache ? 'red' : 'green';
      console.log(snap.metadata.fromCache);
    }});
  </script>
</body>

@milaGGL
Copy link
Contributor

milaGGL commented Jun 18, 2024

@atsjo, thank you for the repro, i am looking into the cause.

@firebase firebase locked and limited conversation to collaborators Jul 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.