firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

enablePersistence not working on Safari v14

Open InputNeuron opened this issue 6 years ago • 37 comments

[REQUIRED] Describe your environment

  • Operating System version: MacOS 10.15.6 (19G2021)
  • Browser version: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15
  • Firebase SDK version: 8.0.2
  • Firebase Product: auth, firestore

[REQUIRED] Describe the problem

When calling enablePersistence in Safari it just locks and blocks and never returns.

The weird thing is if I do it 3-4 times it seems to break out of whatever was locking it up.

Then it works fine and works like my normal app does under Chrome.

However, if I clear all the browser data again, and do it a second time, it will lock up again.

Steps to reproduce:

Probably just clear your cache, and try to enablePerisistence with synchronizeTabs=true

InputNeuron avatar Nov 16 '20 23:11 InputNeuron

synchronizeTabs=false doesn't fix it

The issue is just enablePersistence. With this enabled it won't work.

InputNeuron avatar Nov 17 '20 00:11 InputNeuron

I tried reproduce on Safari Desktop 14 with firebase 8.0.2 with below code:

firebase
      .firestore()
      .enablePersistence({
        synchronizeTabs: true
      }).then(() => {
    db.collection("abc").onSnapshot((snapshot => {
      snapshot.docChanges().forEach(change => {
        console.log(`change type ${change.type}`);
      });
    }));
  });

And it works, as i can see the doc change printed to the console right away. Can you share your code?

wu-hui avatar Nov 17 '20 16:11 wu-hui

I think what I'll do is compile a small little app to reproduce it. It's definitely locking up in enablePersistence. It seems to eventually work at which point it doesn't have this issue anymore.

Did you clear all your data for this origin? Thats' the only way I can reproduce it. It will only work when there's no existing firestore login and no indexeddb data there.

InputNeuron avatar Nov 17 '20 18:11 InputNeuron

This basic code snippet in typescript locks up for us:



        const app = firebase.initializeApp({
            ...conf
        })

        const firestore = firebase.firestore(app);

        const settings = {
            // timestampsInSnapshots: true
            cacheSizeBytes: firebase.firestore.CACHE_SIZE_UNLIMITED
        };

        firestore.settings(settings);

        console.log("Enabling firestore persistence....");
        await firestore.enablePersistence({synchronizeTabs: true});
        console.log("Enabling firestore persistence....done");

InputNeuron avatar Nov 17 '20 18:11 InputNeuron

I can remove CACHE_SIZE_UNLIMITED and synchronizeTabs and it still locks up. It happened on my last MacOS box too but Safari wasn't super important.

InputNeuron avatar Nov 17 '20 18:11 InputNeuron

I just stumbled across this issue. When checking the network tab, no data was send to firestore. When I disable enablePersistence it works just fine.

Version 14.1.1 (16611.2.7.1.4)

firebase
    .firestore()
    .enablePersistence({ synchronizeTabs: true })
    .catch((err) => {
        console.error('Cannot enable firestore offline persistence', err);
    });

boldtrn avatar Jun 21 '21 22:06 boldtrn

@boldtrn I think there might be an interaction with JS console.

What I think might be happening is that the SECOND time you're able to get it to work. Something weird is happening with it just needing to be initialized. Once it's working it just continues to work.

InputNeuron avatar Jun 21 '21 22:06 InputNeuron

related? https://www.theregister.com/2021/06/16/apple_safari_indexeddb_bug/

keeth avatar Jun 30 '21 06:06 keeth

MORE DETAIL TO SHARE THAT I AM OBSERVING

OS/Safari Versions:

  • I encountered an issue where my app hung up getting firestore data a couple of days ago when testing my app development on my iPhone (iOS 14.6).
  • I use a PC and chrome for dev. So, I tested on on of my daughter's old Macs using OS 10.14.2 and Safari 12.0.2 (very out of date), and the issue is there also on these old versions. When I comment out the enablePersistence snippet, then the app works and the firestore get()'s are retrieving data (and, also works on my iPhone).
  • I tested on my other daughter's mac and updated the system to OS 11.4 and Safari 14.1.1, and the issue is on this mac too. Commenting out enablePersistence allows the app to work and firestore get()'s are retrieving data.

So, based on what I've experienced, I don't think its related to the new known Safari bug (still, a very bad thing) mentioned by Keeth.

More on what I'm observing:

  • I have a multi-page app.
  • If I navigate to a page that has a firestore get() and use Safari Develop/EmptyCaches, then: 1) I can work on the page and all of the get() calls work properly, 2) Looking at the network tab, I see a number of firestore related calls. First is getAccountInfo (which may be from an onAuthStateChanged call). Then, there are a number of 'channel' calls, which I believe are related to the get() calls retrieving data.
  • Now, if I navigate to another page that has firestore get() calls, the app gets hung up on the first get(). Looking at the network tab, getAccountInfo completes. But, there is not a single listing of a 'channel' call.
  • Now, if I return to the original page, the app is hung up on the first get(), just like when I navigated to the second page.
  • Now, if I again use Develop/EmptyCaches on the original page, then all of the get() calls on the page work again, just as in the beginning.

Everything is working fine on my PC using Chrome (observing all of the 'channel' calls on the dev tools network tab.

I hope there can be some kind of resolution to this soon. Turning off enablePersistence to get my app to work and not hang is takes away a lot of value to the app.

RERepos avatar Jun 30 '21 14:06 RERepos

@RERepos Can you share debug logs? I can take a quick look tomorrow or on Friday (after which I will be out for a week).

schmidt-sebastian avatar Jun 30 '21 21:06 schmidt-sebastian

@keeth good catch! This sounds like the issue but I've seen this bug way earlier.

I have a potential workaround.

  • check for a localStorage property like 'safari.indexdb.workaround'
  • if it doesn't exist, init firestore, but set a timeout for say 200ms later that redirects the user back to the same app, after we set 'safari.indexdb.workaround' to true.

If it's true that it works the SECOND time this would fix the problem.

I've seen the exact same behavior and at first didn't understand what was happening.

Another idea could be to see if the IndexDB index exists or something along those line.

InputNeuron avatar Jun 30 '21 22:06 InputNeuron

Apparently this is a workaround too:

https://twitter.com/feross/status/1404569098294501380

CleanShot 2021-06-30 at 16 58 34@2x

InputNeuron avatar Jun 30 '21 22:06 InputNeuron

@RERepos Can you share debug logs? I can take a quick look tomorrow or on Friday (after which I will be out for a week).

@schmidt-sebastian,

I'd love to help. But, I'm a self-taught dev. So, I need a little help understanding what you are looking for, and how to obtain it.

Are these logs from the safari console? There is nothing there unless I intentionally insert a console.log. I know that firebase functions have a log, but I don't see a log for firestore?

RERepos avatar Jun 30 '21 23:06 RERepos

@RERepos You just need to invoke firebase.firestore.setLogLevel('debug'). This will print the logs to the browser console.

schmidt-sebastian avatar Jul 01 '21 00:07 schmidt-sebastian

@schmidt-sebastian

Here are the logs: [Log] [2021-07-01T04:49:51.030Z] @firebase/firestore: – "Firestore (8.6.8): FirebaseCredentialsProvider" – "Auth detected" (firebase-firestore.js, line 1) [Log] [2021-07-01T04:49:51.315Z] @firebase/firestore: – "Firestore (8.6.8): FirestoreClient" – "Received user=" – "8lG935Ld5mNEmKEGDwsJg9REmfK2" (firebase-firestore.js, line 1) [Log] [2021-07-01T04:49:51.315Z] @firebase/firestore: – "Firestore (8.6.8): FirestoreClient" – "Initializing OfflineComponentProvider" (firebase-firestore.js, line 1) [Log] [2021-07-01T04:49:51.316Z] @firebase/firestore: – "Firestore (8.6.8): IndexedDbPersistence" – "Starting transaction:" – "updateClientMetadataAndTryBecomePrimary" (firebase-firestore.js, line 1) [Log] [2021-07-01T04:49:51.316Z] @firebase/firestore: – "Firestore (8.6.8): SimpleDb" – "Opening database:" – "firestore/[DEFAULT]/fretdrills/main" (firebase-firestore.js, line 1) [Log] [2021-07-01T04:55:24.894Z] @firebase/firestore: – "Firestore (8.6.8): AsyncQueue" – "Visibility state changed to hidden" (firebase-firestore.js, line 1) [Log] [2021-07-01T04:57:17.819Z] @firebase/firestore: – "Firestore (8.6.8): AsyncQueue" – "Visibility state changed to visible" (firebase-firestore.js, line 1)

RERepos avatar Jul 01 '21 04:07 RERepos

@RERepos Are those all the logs you have? If so, then it looks like the SDK gets stuck waiting for IndexedDB. We could put a time limit on IndexedDB and continue without persistence if it does not respond within a certain amount of time. Would that work for your use case?

schmidt-sebastian avatar Jul 01 '21 16:07 schmidt-sebastian

@schmidt-sebastian

Thank you for your reply!

  1. "Are those all the logs you have?"
  • Yes, on Safari. On Chrome (on PC), the log is quite long.
  1. "We could put a time limit on IndexedDB and continue without persistence if it does not respond within a certain amount of time. Would that work for your use case?"
  • I believe so. I hope that this would allow me to keep the enablePersistence code snippet, so that users from the non-Apple world can get the value from persistence and work offline, while the users from the Apple world would just have to deal with working online only. I've been trying work-arounds to no avail, so my alternative to prevent bricking people from the Apple world is to turn off persistence for everyone.

RERepos avatar Jul 01 '21 16:07 RERepos

Thanks for the quick reply. Sounds like we have a path forward then. I will try to get this fixed before the end of the week (I am out next week).

schmidt-sebastian avatar Jul 01 '21 16:07 schmidt-sebastian

@RERepos @schmidt-sebastian I think a time based bound is not the right strategy and would be considered harmful and actively a BAD solution.

Please do not do this.

  1. if the machine is heavily loaded at the time you're going to disable caching just because the computer is under heavy load. That's wrong and will create bugs. We're an offline app and when we tell you to go offline I don't expect magic to happen and it revert to not working. Locking up is preferred actually.

  2. There are other bugs related to Firestore being slow to init IndexedDB since it doesn't use indexes. That will trigger you to kick in this fix thus causing other bugs.

Can you try this instead?

image

... you should try this approach FIRST.

You would put this as the first line of the Firestore SDK so that an import() require() would immediately reference IndexedDB thus bypassing the bug.

That's a much cleaner way of resolving this.

This is really impacting us and we can't ship our iOS app without this fixed so I don't mind giving you feedback on how this is working as a resolution.

We're going to try this in our code since we don't rely on Firestore and I'll try to follow up once we have feedback.

But please please please don't to the above as that's actively harmful. Or at LEAST allow us to disable that behavior.

InputNeuron avatar Jul 01 '21 17:07 InputNeuron

I'm very sympathetic and understanding, that everyone may have different needs.

"we don't rely on Firestore"

My situation is that I do rely on Firestore ...

I thank everyone for their understanding, and hope there can be a solution for everyone's needs ...

RERepos avatar Jul 01 '21 17:07 RERepos