0

The Firestore enableIndexedDbPersistence allows us to specify forceOwnership: true to specify that we're in a service worker context. This works great in MV3 Chrome Extension service worker. It starts the IndexedDB and I'm able to view the stored data in extension context.

However, when I call clearIndexedDbPersistence, it fails because of "window is not defined" errors. The exact trace is this but it is probably not relevant:

Uncaught (in promise) ReferenceError: window is not defined
    at Yl.delete (index.esm2017.js:6055:59)
    at index.esm2017.js:16720:26
    at index.esm2017.js:16717:19
    at index.esm2017.js:16308:54

To repro the issue, simply call clearIndexedDbPersistence() a few seconds after the call to enableIndexedDbPersistence(firebase, { forceOwnership: true }) has finished, in a service worker in a MV3 Chrome extension.

Is there a temporary workaround, or a flag in Firestore, that will make this work? I did not find any open issues about this, so it is possible I am missing something, but I don't see what that could be.

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
  • It's a bug in firebase. See if they have a js file for service workers. If not, report the bug. A possible workaround may be `self.window = self` before importing their js - it'll work only in case they mistakenly used `window` to address the global scope and not for its DOM features. – wOxxOm Jul 22 '22 at 09:28
  • Some helpful links here [1] (https://stackoverflow.com/questions/49664665/window-is-not-defined-service-worker ) [ 2 ] (https://stackoverflow.com/questions/20136898/uncaught-referenceerror-window-is-not-defined ) [3] (https://stackoverflow.com/questions/68194103/error-in-event-handler-referenceerror-window-is-not-defined-chrome-extension-w) – Vaidehi Jamankar Jul 22 '22 at 12:30
  • @wOxxOm Thanks I'll report it in their github repo. – Gaurang Tandon Jul 22 '22 at 13:37
  • @VaidehiJamankar Thanks but all the links are unrelated to this particular issue. – Gaurang Tandon Jul 22 '22 at 13:43

0 Answers0