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

runTransaction doesn't work in Chrome extension MV3

Open tarasinf opened this issue 3 years ago • 9 comments

  • Operating System version: Ubuntu 20.04.3 LTS
  • Browser version: 102.0.5005.61
  • Firebase SDK version: 9.6.5
  • Firebase Product: firestore
  • Chrome extension: VM3

The problem

I can't use runTransaction in Chrome extension MV3 as it doesn't support XMLHttpRequest. All other commands work well. I found that it was fixed on GH (for some reasons, not for transactions).

Steps to reproduce:

    let userRef = doc(db, 'users', userInfo.id);
    runTransaction(db, transaction => {
      return transaction.get(userRef).then(userDoc => {
        return transaction.update(userRef, { 'eventName': 42 });
      });
    });

I got an error: Uncaught (in promise) FirebaseError: ReferenceError: XMLHttpRequest is not defined

My assumption is that V9 still uses XMLHttpRequest in runTransaction.

tarasinf avatar Jul 28 '22 06:07 tarasinf

Thanks for bringing this to our attention. This is in my queue to investigate. In the meantime, is it possible for you to provide us with a minimal reproducible example so we can quickly reproduce the issue in same environment your code is executing in?

MarkDuckworth avatar Jul 28 '22 23:07 MarkDuckworth

@MarkDuckworth thank you for the quick response. I can prepare a small chrome expansion project. Will it work for you? (Or, in case it's possible, you can try to run the code above under some env that doesn't support XMLHttpRequest.)

tarasinf avatar Jul 29 '22 08:07 tarasinf

@MarkDuckworth here is a small Chrome extension example with firebase call. To run it:

  • update values in manifest.json:, extension_ids, client_id, key
  • update firebaseConfig in bakground.js
  • import extension, click on the icon, press button.
  • in service worker debug tool you will be able to see the error Uncaught (in promise) FirebaseError: ReferenceError: XMLHttpRequest is not defined

I hope you will find an easier way to replicate the issue, as an extension env looks a bit complicated.

tarasinf avatar Jul 29 '22 18:07 tarasinf

Thank you. I was able to reproduce this using the code you provided. I have moved the bug to our internal tracking system and will update you when we have a fix planned.

In the meantime, are you able to work around this without using transactions?

MarkDuckworth avatar Jul 29 '22 20:07 MarkDuckworth

Thank you @MarkDuckworth, It would be much, much better to have transactions. I am going to implement a counter (per user) to know the number of items. Without transactions, I am doing .size() - what's a heavy operation.

Do you have any ideas, how long will it tale to implement the fix?

tarasinf avatar Jul 29 '22 20:07 tarasinf

I'll have to follow up with you next week regarding the timeline of a fix.

MarkDuckworth avatar Jul 29 '22 21:07 MarkDuckworth

I reviewed this in depth today. The required changes are non-trivial. At this point I can only say that this is in our backlog to be scheduled in a future sprint.

However, I did notice that Firestore Lite uses fetch() instead of XMLHttpRequest. I was able use runTransaction in a service worker of a MV3 Chrome extension when using Firestore Lite.

Does Firestore Lite meet the needs for your use case?

MarkDuckworth avatar Aug 01 '22 23:08 MarkDuckworth

Tracking b/241154587 and b/241157202

MarkDuckworth avatar Aug 02 '22 22:08 MarkDuckworth

Thank you, I will try to migrate to Firestore Lite, meantime.

tarasinf avatar Aug 03 '22 19:08 tarasinf

I get this same error message of XMLHttpRequest not defined when trying to use uploadBytesResumable or uploadBytes methods from firebase-storage

Owillz01 avatar Feb 28 '23 22:02 Owillz01

Hey there, are there any updates on this issue? I am unfortunately experiencing the same problem and can't migrate into Firestore Lite at the moment.

capybarahero avatar Sep 11 '23 11:09 capybarahero

@capybarahero Jumping in while @MarkDuckworth is on vacation.

We are still tracking this. However, we are in the midst of planning new query features, so I don't feel comfortable making any promises at this time. But I will raise this with the team, as we might be able to find some resources to deprecate usage of XMLHttpRequest.

Hang tight, we know this is a limitation when using extensions and service workers.

tom-andersen avatar Sep 11 '23 17:09 tom-andersen