Skip to content

Commit 1601572

Browse files
authored
fix error when initializing firebase on service worker (#8414)
1 parent 08bb87b commit 1601572

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

.changeset/slow-emus-vanish.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/app-compat': patch
3+
---
4+
5+
Updated how app-compat checks the global scope.

packages/app-compat/src/index.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { FirebaseNamespace } from './public-types';
19-
import { isBrowser } from '@firebase/util';
19+
import { getGlobal } from '@firebase/util';
2020
import { firebase as firebaseNamespace } from './firebaseNamespace';
2121
import { logger } from './logger';
2222
import { registerCoreComponents } from './registerCoreComponents';
@@ -27,22 +27,28 @@ declare global {
2727
}
2828
}
2929

30-
// Firebase Lite detection
31-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
32-
if (isBrowser() && window.firebase !== undefined) {
33-
logger.warn(`
34-
Warning: Firebase is already defined in the global scope. Please make sure
35-
Firebase library is only loaded once.
36-
`);
37-
38-
// eslint-disable-next-line
39-
const sdkVersion = (window.firebase as FirebaseNamespace).SDK_VERSION;
40-
if (sdkVersion && sdkVersion.indexOf('LITE') >= 0) {
30+
try {
31+
const globals = getGlobal();
32+
// Firebase Lite detection
33+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
34+
if ((globals as any).firebase !== undefined) {
4135
logger.warn(`
42-
Warning: You are trying to load Firebase while using Firebase Performance standalone script.
43-
You should load Firebase Performance with this instance of Firebase to avoid loading duplicate code.
36+
Warning: Firebase is already defined in the global scope. Please make sure
37+
Firebase library is only loaded once.
4438
`);
39+
40+
// eslint-disable-next-line
41+
const sdkVersion = ((globals as any).firebase as FirebaseNamespace)
42+
.SDK_VERSION;
43+
if (sdkVersion && sdkVersion.indexOf('LITE') >= 0) {
44+
logger.warn(`
45+
Warning: You are trying to load Firebase while using Firebase Performance standalone script.
46+
You should load Firebase Performance with this instance of Firebase to avoid loading duplicate code.
47+
`);
48+
}
4549
}
50+
} catch {
51+
// ignore errors thrown by getGlobal
4652
}
4753

4854
const firebase = firebaseNamespace;

0 commit comments

Comments
 (0)