From 2cf68c3c24f89ba39597c7a35257e898fa564838 Mon Sep 17 00:00:00 2001 From: jbalidiong Date: Thu, 8 Aug 2024 17:57:15 +0800 Subject: [PATCH 1/4] fix error when initializing firebase on service worker --- packages/app-compat/src/index.ts | 33 ++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/packages/app-compat/src/index.ts b/packages/app-compat/src/index.ts index 8d4a7834a7e..1cb9c3814ca 100644 --- a/packages/app-compat/src/index.ts +++ b/packages/app-compat/src/index.ts @@ -16,7 +16,7 @@ */ import { FirebaseNamespace } from './public-types'; -import { isBrowser } from '@firebase/util'; +import { isBrowser, getGlobal } from '@firebase/util'; import { firebase as firebaseNamespace } from './firebaseNamespace'; import { logger } from './logger'; import { registerCoreComponents } from './registerCoreComponents'; @@ -27,22 +27,27 @@ 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; From db71965ddfc9e21c3d34f247ed6d0556f00e1912 Mon Sep 17 00:00:00 2001 From: jbalidiong Date: Thu, 8 Aug 2024 19:09:40 +0800 Subject: [PATCH 2/4] update changeset comment --- .changeset/slow-emus-vanish.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/slow-emus-vanish.md diff --git a/.changeset/slow-emus-vanish.md b/.changeset/slow-emus-vanish.md new file mode 100644 index 00000000000..dcb6aa2f1fa --- /dev/null +++ b/.changeset/slow-emus-vanish.md @@ -0,0 +1,5 @@ +--- +'@firebase/app-compat': patch +--- + +Updated how app-compat checks the global scope. From 6025cbc945fa847d11722a615c5a54f83cb2d6f3 Mon Sep 17 00:00:00 2001 From: jbalidiong Date: Thu, 8 Aug 2024 19:31:43 +0800 Subject: [PATCH 3/4] run linter --- packages/app-compat/src/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/app-compat/src/index.ts b/packages/app-compat/src/index.ts index 1cb9c3814ca..77a05865abe 100644 --- a/packages/app-compat/src/index.ts +++ b/packages/app-compat/src/index.ts @@ -16,7 +16,7 @@ */ import { FirebaseNamespace } from './public-types'; -import { isBrowser, getGlobal } from '@firebase/util'; +import { getGlobal } from '@firebase/util'; import { firebase as firebaseNamespace } from './firebaseNamespace'; import { logger } from './logger'; import { registerCoreComponents } from './registerCoreComponents'; @@ -39,14 +39,14 @@ try { // eslint-disable-next-line const sdkVersion = ((globals as any).firebase as FirebaseNamespace).SDK_VERSION; - if (sdkVersion && sdkVersion.indexOf('LITE') >= 0) { - logger.warn(` + 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{ +} catch { // ignore errors thrown by getGlobal } From 4b5337140200822617a5b786ec7800e5716eb5e6 Mon Sep 17 00:00:00 2001 From: jbalidiong Date: Thu, 8 Aug 2024 19:43:58 +0800 Subject: [PATCH 4/4] run formatter --- packages/app-compat/src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/app-compat/src/index.ts b/packages/app-compat/src/index.ts index 77a05865abe..ffdec982afb 100644 --- a/packages/app-compat/src/index.ts +++ b/packages/app-compat/src/index.ts @@ -38,7 +38,8 @@ try { `); // eslint-disable-next-line - const sdkVersion = ((globals as any).firebase as FirebaseNamespace).SDK_VERSION; + 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.