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

clearIndexedDbPersistence gives 'window is not defined' error in service worker

Open GaurangTandon opened this issue 3 years ago • 4 comments

Environment

  • Operating System version: Windows 10/Ubuntu 22.04
  • Browser version: Chrome 103/104/105
  • Firebase SDK version: 9.9.0
  • Firebase Product: firestore

Problem

Steps to reproduce

  1. Setup a Manifest V3 Chrome Extension with a service worker. (not necessary, see notes -> 1)
  2. Use enableIndexedDbPersistence(firebase, { forceOwnership: true }) in the service worker to setup persistence.
  3. Verify in the storage tab that indexeddb storage is enabled, great!
  4. Now, call clearIndexedDbPersistence() to remove all data from the indexeddb.

Actual result

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

Expected result

The indexed DB should be cleared.

Notes

  1. I don't think the problem is specific to MV3 Chrome extensions, but I don't have any other service worker environment to try this in.
  2. I had earlier asked a StackOverflow question for this.

GaurangTandon avatar Jul 22 '22 13:07 GaurangTandon

Hi team! Is there any update on this issue? In the mean time, is there any other alternative code so as to manually clear the Firebase IndexedDB ourselves?

GaurangTandon avatar Jan 21 '24 10:01 GaurangTandon

Hi @GaurangTandon , sorry for the long wait on this ticket.

I assume you are using a newer version of the Firebase SDK( not the v9.9.0 in the description) and the problem still exists, correct?

Could you please provide a minimal repro app to reproduce this error?

milaGGL avatar Jan 22 '24 19:01 milaGGL

Hi @milaGGL I am still able to reproduce the issue on the latest version. The stack trace has changed to this now:

Uncaught (in promise) ReferenceError: window is not defined
    at ts.delete (index.esm2017.js:1664:95)
    at index.esm2017.js:18823:42
    at index.esm2017.js:18820:19
    at index.esm2017.js:18400:54

I have added a minimal repro at this drive link: https://drive.google.com/file/d/1QUBEMtaoEf4SHSa2j3yORWn739nDd4_9/view?usp=sharing

After you download the zip file, here's a video showing step by step to repro the issue: https://github.com/firebase/firebase-js-sdk/assets/6308683/69f6f094-b7db-43f6-b34a-7879717a379c

You can swap manifest.json contents with mv2.json/mv3.json (as I have done in the video) and observe the error in mv3 version. This is because mv3 version uses a service worker (which doesn't have a window defined globally), whereas mv2 version does have window defined globally.

GaurangTandon avatar Jan 23 '24 07:01 GaurangTandon

@GaurangTandon, I am able to reproduce this error, will start looking into the root cause ASAP.

milaGGL avatar Jan 23 '24 15:01 milaGGL