Visitar URL original
FR: Firestore offline persistence in web workers · Issue #983 · firebase/firebase-js-sdk · GitHub
Skip to content

FR: Firestore offline persistence in web workers #983

Closed

Description

@lamplightdev
  • Operating System version: n/a
  • Browser version: n/a
  • Firebase SDK version: n/a
  • Firebase Product: firestore

Are there any plans to make offline persistence available in web workers? Inspired by @davideast 's article I dropped 95% of my Firebase bundle size using this one weird trick to use firebase in a worker, I've noticed offline persistence isn't available in that context. Browsing the source, I suspect it's the reliance on (and absence in workers of) localStorage that's the issue - so I was wondering if there's any alternative to using synchronous storage in this case? Thanks.

Activity

  1. joshdifabio commented on Jan 14, 2019

    @joshdifabio

    Any news on this guys? The lack of offline persistence in service workers & web workers is a bit of a killer for PWA.

  2. MarvinMiles commented on Jan 16, 2019

    @MarvinMiles

    Same here

  3. mikelehen commented on Jan 16, 2019

    @mikelehen
    Contributor

    Thanks for the feedback. Could folks impacted by this tell us a little bit about their use case / architecture (what kind of worker they're trying to use Firestore from and for what purpose exactly)?

  4. joshdifabio commented on Jan 17, 2019

    @joshdifabio

    @mikelehen thanks for responding.

    In my case, the data I pull from Firestore sometimes needs quite a lot of processing before I present it to users. As a result, I do most of my Firestore interaction inside a Web Worker instead of on the main thread in order to reduce UI jank. I could move this work to the Service Worker instead but I assume that this Firestore SDK limitation applies to Service Workers too since they also do not support the localStorage API.

    Unfortunately, this approach means that I lose offline persistence in my app, even though workers support IndexedDB.

    As the Service Worker API in particular becomes more popular I imagine that this will become a problem for more people.

  5. mikelehen commented on Jan 17, 2019

    @mikelehen
    Contributor

    Thanks for your use case. One option would of course be to pull the data from Firestore on the main event loop and then just do the processing on the webworker, but if it's a lot of data, perhaps there are performance wins for moving the Firestore interaction off the main event loop as well. So it's a reasonable request.

    Service workers are a bit more complicated. They don't support XHR, which Firestore (and possibly other parts of Firebase) depends on. And service worker lifetime is generally short-lived. Our supposition so far is that it doesn't make that much sense to use Firestore from a service worker. But if folks have use cases, please do share.

  6. joshdifabio commented on Jan 18, 2019

    @joshdifabio

    Thanks for your use case. One option would of course be to pull the data from Firestore on the main event loop and then just do the processing on the webworker

    Thanks. I did consider this, but it wasn't practical for a few reasons. For example:

    1. This approach would significantly increase the amount of boilerplate code I'd need to write in order to get data in and out of the worker thread, as the worker thread often decides which queries to send to Firestore based on the results of other queries
    2. When processing Firestore data I often need access to Firestore's snapshot objects in order to process the data more efficiently (e.g. using DocumentSnapshot.get()), but class instances cannot be passed between the main thread and workers.
  7. joshdifabio commented on Jan 22, 2019

    @joshdifabio

    Something I forgot to add previously: every now and then I see the main thread blocked for 100ms+ by Firestore XHR-related scripts shortly after cancelling snapshot listeners. (Note that this timing is taken on a powerful MacBook Pro and I'd expect the main thread to be blocked for much longer on mobile.) This is another reason I offload most of my Firestore interaction to a worker thread.

  8. mikelehen commented on Jan 22, 2019

    @mikelehen
    Contributor

    @joshdifabio That sounds pretty strange. Offhand I can't think of anything CPU-intensive we do when unsubscribing (and XHRs themselves are async). How exactly are you measuring the main thread blocking? Thanks!

  9. joshdifabio commented on Jan 23, 2019

    @joshdifabio

    @mikelehen interesting. I observed jank and then used the performance profiler in Chrome. I haven't looked at it in detail yet, I only really saw that Firestore XHR was at the top -- perhaps it was GC or something in my own code being triggered. I'll have another look soonish and get back to you.

  10. dexster commented on Feb 19, 2019

    @dexster

    My use case is for offline updates to be saved to Firestore without having to open the relevant web site when online ie. like web push where only the browser is required to be open. I assume due to localstorage API unavailability, this will not be possible

  11. mikelehen commented on Feb 21, 2019

    @mikelehen
    Contributor

    @dexster I don't think any kind of worker will be able to run when the relevant web site isn't open. For that you'd need a browser extension or something.

  12. joshdifabio commented on Feb 22, 2019

    @joshdifabio

    I've done some more performance profiling over the past couple of weeks, and this is what I've observed regarding Firestore performance.

    Whenever I read data from Firestore, I can see about about 50-150ms spent in Firestore code when the XHR response comes back. Everything happening in that time period appears to be Firebase code. Firestore processes the response synchronously on the main thread, and obviously even the lower bound of this figure (50ms) is way beyond what can realistically be accommodated on the main thread without introducing UI jank. These times were measured on a MacBook Pro; the times are higher on my Pixel 2.

    I previously said that this work was happening whenever I cancelled Firestore snapshot listeners, but this was a mistake, it's when the data comes back. I see this all the time, it's not an edge case -- 50ms of work in the main thread is the lower bound when listening to a single record.

    Unless I'm reading the figures wrong in Chrome's profiler and including my own application code in the figures, which I don't think I am, I suspect that these figures will be easily reproducible on any web app which uses the Firestore JS SDK.

  13. mikelehen commented on Mar 1, 2019

    @mikelehen
    Contributor

    @joshdifabio Thanks for the added details. I was able to reproduce ~50ms with a big document in Chrome on my Mac. The time was basically spent parsing the result from the backend.

    Note that I believe I made a recent size optimization (4983420) that will also improve performance a little (it removed some logging calls that should have an impact here). That should go out in our next release.

    All that said, 50ms for a large document doesn't seem unreasonable to me. At 60fps, you might skip a couple frames, but for a typical web app, this should be imperceivable. A live-action game might be a different story. In any case, if you're sensitive to these sorts of hiccups, moving the processing to a separate worker seems like a reasonable approach. Sorry that persistence doesn't work at the moment. We'll give this some thought in the future.

  14. 40 remaining items

  15. thebrianchen commented on Jan 8, 2020

    @thebrianchen

    @scottfr That's exciting news! In your extension, does passing in experimentalForce: true allow persistence to work for you without any additional code, or do you have to add logic to release the persistence lock in edge cases?

    Thanks for bringing up web locks -- we will definitely consider adding support once we determine the viability of experimentalForce.

  16. raphaelbs commented on Apr 5, 2020

    @raphaelbs

    Hi everyone. +1 interested in this feature.

    Could folks impacted by this tell us a little bit about their use case / architecture (what kind of worker they're trying to use Firestore from and for what purpose exactly)?

    In our case, we have an application that uses only Firestore and Auth, just to read and write user data, nothing fancy.
    Even though, I'd just migrated the entirety of Firebase to a worker and the application has shown a significant increase in performance for its entry pages:

    • avg -2.8s for First CPU idle
    • avg -1.6s for Speed Index
    • avg -1.1s for Minimize main-thread work
    • avg -0.7s for Reduce JavaScript execution time

    I got those numbers running synthetic tests with Lighthouse server for the branch I'm currently working on.

    Is there a way to ship the experimentalForce flag into the SDK so we can validate the data in production?
    We also use the offline persistence (the app is a PWA) and would be ideal not to lose that.

  17. thebrianchen commented on Apr 9, 2020

    @thebrianchen

    I will look into adding this as an experimental feature in the coming weeks. Thanks for sharing the feedback!

  18. laurentpayot commented on Apr 16, 2020

    @laurentpayot

    +1 for this as an experimental feature of the official Firestore releases. It's getting harder and harder to make custom builds work along with always changing Firebase products (modules location, types location, testing, etc.)

  19. tsavo-vdb-knott commented on Apr 18, 2020

    @tsavo-vdb-knott

    Case for this:

    Worker Driven Design ~ 3:41 ~

    Screen Shot 2020-04-18 at 5 16 39 AM

    Appreciate all the work Firebase is doing and I - as a development platform author of Runtime.dev am making it a first class citizen - Web worker support is paramount to data driven apps :)

    Cheers,
    -Tsavo

  20. marianopaulin02 commented on Apr 19, 2020

    @marianopaulin02

    Im also using the trick of moving firebase-related logic to a service worker in order to have just 1 instance and 1 single connection to Firebase Realtime DB even when multiple app tabs are open, and Im keeping the worker alive via sending 10-seconds periodical empty messages. Now I was trying to implement Firestore and found a limitation in the service worker because this one doestn't support XMLHttpRequest (used for Firestore as mentioned before in the thread). There is a way to implement Fetch instead, or maybe a wrapper?

    image
    I got this error running a simple db.collection(..).doc(..).get() operation
    Now i'm using Firebase 7.14.1 via importScripts

  21. laurentpayot commented on Jun 20, 2020

    @laurentpayot

    So far the experimentalForceOwningTab option is working perfectly, thanks @thebrianchen for all the work! 👍

  22. thebrianchen commented on Aug 12, 2020

    @thebrianchen

    Hey everyone! This experimental feature has been out for a few months now, and we're hoping to collect some usage feedback to see how this feature has been working for you to see if/how we should support this in the long term. In particular, how are you using experimentalForceOwningTab? Which workers are you using this with, and how much additional code are you writing to make Firestore requests? Thanks!

  23. laurentpayot commented on Aug 14, 2020

    @laurentpayot

    @thebrianchen It's been working great so far. I use dedicated workers, with this code to detect multi tab usage and disable persistence for new tabs.

  24. jwhiting commented on Feb 9, 2021

    @jwhiting

    @thebrianchen thank you for the feature. FYI I am using experimentalForceOwningTab for the use case of a personal information management Chrome extension. The architecture uses a web worker spawned by the background page, and popup and full-page tab UIs. The UI pages use message passing to the background context, which then delegates (also via messaging) all storage and processing tasks to the web worker. The main reason to use the web worker is to prevent UI jank, as the storage and processing tasks can be fairly computationally expensive, and are fired often. Consider a rich text editor with a large document: serializing and storing this document, even when done every N keystrokes via a debounced/throttled callback, can still interrupt the natural responsiveness of keystrokes. We also use local javascript to compute a fulltext index on the documents to enable offline fulltext search, and updating this index is expensive (another reason for a web worker, though not specific to Firestore persistence, but it is certainly nice to be able to do all the heavy lifting in one context. In the future we might investigate persisting the fulltext index to Firestore as well.)

  25. stroobandt commented on Apr 14, 2021

    @stroobandt

    Would it not be better and fairer to offer persistence for any web worker on Chrome for Android, independently of whether firebase is in use or not?

    My use case is recurring PWA notifications, as detailed in this open issue with the Google Chrome for Android PWA team. The lack of web worker persistence on Chrome for Android is currently the only thing standing in the way of an implementation.

    It really looks like corporate Google engineers of different Google divisions are waging similar battles against a common problem.

    Moreover, according to the spec, I would be inclined to say: we are dealing here with a Chrome for Android bug. The reason being: A web worker with outstanding timers, database transactions, or network connections should be treated as a protected worker.

  26. ReaperTech commented on Jul 25, 2021

    @ReaperTech

    Im also using the trick of moving firebase-related logic to a service worker in order to have just 1 instance and 1 single connection to Firebase Realtime DB even when multiple app tabs are open, and Im keeping the worker alive via sending 10-seconds periodical empty messages. Now I was trying to implement Firestore and found a limitation in the service worker because this one doestn't support XMLHttpRequest (used for Firestore as mentioned before in the thread). There is a way to implement Fetch instead, or maybe a wrapper?

    image
    I got this error running a simple db.collection(..).doc(..).get() operation
    Now i'm using Firebase 7.14.1 via importScripts

    @marianopaulin02 does this page help? https://stackoverflow.com/questions/63261382/writing-data-in-service-worker-to-cloud-firestore

  27. thebrianchen commented on Jan 20, 2022

    @thebrianchen

    Closing this issue as the feature has been launched a while ago. Feel free to open a new issue if there are any other questions or issues!

  28. locked and limited conversation to collaborators on Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions