Skip to content

Commit

Permalink
fix error when initializing firebase on service worker (#8414)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbalidiong authored Aug 8, 2024
1 parent 08bb87b commit 1601572
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-emus-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/app-compat': patch
---

Updated how app-compat checks the global scope.
34 changes: 20 additions & 14 deletions packages/app-compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { FirebaseNamespace } from './public-types';
import { isBrowser } from '@firebase/util';
import { getGlobal } from '@firebase/util';
import { firebase as firebaseNamespace } from './firebaseNamespace';
import { logger } from './logger';
import { registerCoreComponents } from './registerCoreComponents';
Expand All @@ -27,22 +27,28 @@ declare global {
}
}

// Firebase Lite detection
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (isBrowser() && window.firebase !== undefined) {
logger.warn(`
Warning: Firebase is already defined in the global scope. Please make sure
Firebase library is only loaded once.
`);

// eslint-disable-next-line
const sdkVersion = (window.firebase as FirebaseNamespace).SDK_VERSION;
if (sdkVersion && sdkVersion.indexOf('LITE') >= 0) {
try {
const globals = getGlobal();
// Firebase Lite detection
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((globals as any).firebase !== undefined) {
logger.warn(`
Warning: You are trying to load Firebase while using Firebase Performance standalone script.
You should load Firebase Performance with this instance of Firebase to avoid loading duplicate code.
Warning: Firebase is already defined in the global scope. Please make sure
Firebase library is only loaded once.
`);

// eslint-disable-next-line
const sdkVersion = ((globals as any).firebase as FirebaseNamespace)
.SDK_VERSION;
if (sdkVersion && sdkVersion.indexOf('LITE') >= 0) {
logger.warn(`
Warning: You are trying to load Firebase while using Firebase Performance standalone script.
You should load Firebase Performance with this instance of Firebase to avoid loading duplicate code.
`);
}
}
} catch {
// ignore errors thrown by getGlobal
}

const firebase = firebaseNamespace;
Expand Down

0 comments on commit 1601572

Please sign in to comment.