diff --git a/.changeset/spicy-dragons-pay.md b/.changeset/spicy-dragons-pay.md new file mode 100644 index 00000000000..280406da0f0 --- /dev/null +++ b/.changeset/spicy-dragons-pay.md @@ -0,0 +1,5 @@ +--- +'@firebase/app-compat': patch +--- + +Properly handle the case in app-compat checks where `window` exists but `self` does not. (This occurs in Ionic Stencil's Jest preset.) diff --git a/packages/app-compat/src/index.ts b/packages/app-compat/src/index.ts index 42b5f5a154a..8d4a7834a7e 100644 --- a/packages/app-compat/src/index.ts +++ b/packages/app-compat/src/index.ts @@ -21,16 +21,22 @@ import { firebase as firebaseNamespace } from './firebaseNamespace'; import { logger } from './logger'; import { registerCoreComponents } from './registerCoreComponents'; +declare global { + interface Window { + firebase: FirebaseNamespace; + } +} + // Firebase Lite detection // eslint-disable-next-line @typescript-eslint/no-explicit-any -if (isBrowser() && (self as any).firebase !== undefined) { +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 = ((self as any).firebase as FirebaseNamespace).SDK_VERSION; + const sdkVersion = (window.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. diff --git a/packages/util/src/environment.ts b/packages/util/src/environment.ts index 30fc661bfc6..998000308c3 100644 --- a/packages/util/src/environment.ts +++ b/packages/util/src/environment.ts @@ -80,6 +80,9 @@ export function isNode(): boolean { /** * Detect Browser Environment + * Note: This will return true for certain test frameworks that are incompletely + * mimicking a browser, and should not lead to assuming all browser APIs are + * available. */ export function isBrowser(): boolean { return typeof window !== 'undefined' || isWebWorker();