Skip to content
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

fix error when initializing firebase on service worker #8414

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading