Skip to content

Commit

Permalink
Fix isWindowSupported() check (#5957)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 committed Feb 1, 2022
1 parent 8b51375 commit 0a04a1c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tame-apes-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/messaging': patch
---

Fix uncaught rejection in `isSupported()` if environment does not support IndexedDB's `open()` method.
2 changes: 2 additions & 0 deletions packages/messaging-compat/src/messaging-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export function isSupported(): boolean {

/**
* Checks to see if the required APIs exist.
* Unlike the modular version, it does not check if IndexedDB.open() is allowed
* in order to keep isSupported() synchronous and maintain v8 compatibility.
*/
function isWindowSupported(): boolean {
return (
Expand Down
8 changes: 7 additions & 1 deletion packages/messaging/src/api/isSupported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ import {
* @public
*/
export async function isWindowSupported(): Promise<boolean> {
try {
// This throws if open() is unsupported, so adding it to the conditional
// statement below can cause an uncaught error.
await validateIndexedDBOpenable();
} catch (e) {
return false;
}
// firebase-js-sdk/issues/2393 reveals that idb#open in Safari iframe and Firefox private browsing
// might be prohibited to run. In these contexts, an error would be thrown during the messaging
// instantiating phase, informing the developers to import/call isSupported for special handling.
return (
typeof window !== 'undefined' &&
isIndexedDBAvailable() &&
(await validateIndexedDBOpenable()) &&
areCookiesEnabled() &&
'serviceWorker' in navigator &&
'PushManager' in window &&
Expand Down

0 comments on commit 0a04a1c

Please sign in to comment.