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

Handle app-compat environment check where self does not exist #8378

Merged
merged 4 commits into from
Jul 24, 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/spicy-dragons-pay.md
Original file line number Diff line number Diff line change
@@ -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.)
10 changes: 8 additions & 2 deletions packages/app-compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions packages/util/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading