Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
GaurangTandon opened this issue Jul 22, 2022 · 9 comments · Fixed by #8888
Closed
Assignees

Comments

@GaurangTandon
Copy link

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
Copy link
Author

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?

@milaGGL
Copy link
Contributor

milaGGL commented Jan 22, 2024

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?

@GaurangTandon
Copy link
Author

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.

@milaGGL
Copy link
Contributor

milaGGL commented Jan 23, 2024

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

@GaurangTandon
Copy link
Author

Hi @milaGGL just checking in to see if there was any update on this issue?

@GaurangTandon
Copy link
Author

I think if we change this line:

return wrapRequest<void>(window.indexedDB.deleteDatabase(name)).toPromise();

to:

  return wrapRequest<void>(indexedDB.deleteDatabase(name)).toPromise();

That would fix the problem. Note that we reference indexedDB without window. prefix at line 295:

const request = indexedDB.open(this.name, this.version);

So it would make sense to do the same on line 166. What do you think?

@GaurangTandon
Copy link
Author

Hi! Sorry for bumping this again, but it looks like this is a relatively simple one-line change that would fix the issue. If that sounds fine, I'm happy to send a PR for this alongside some tests. Please let me know.

@milaGGL
Copy link
Contributor

milaGGL commented Apr 1, 2025

Hi @GaurangTandon, the fix is merged and should be available with the next release. Thank you for reporting this issue and looking into the cause. Please let me know if the upcoming new release has resolved the issue.

@GaurangTandon
Copy link
Author

Thanks a lot for the swift response @milaGGL ! 😄 I will try the new release as soon as it's available

@firebase firebase locked and limited conversation to collaborators May 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants