Skip to content

Commit

Permalink
fix: browser detection
Browse files Browse the repository at this point in the history
update browser detection logic:
detect either window or web worker (WorkerGlobalScope).

fixes #8299 #8284
  • Loading branch information
JoseVSeb committed Jun 26, 2024
1 parent a90255a commit baa96ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/chilly-moons-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/util': patch
---

fix: browser detection (detect either window or web worker)
18 changes: 17 additions & 1 deletion packages/util/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import { CONSTANTS } from './constants';
import { getDefaults } from './defaults';

/**
* Type placeholder for `WorkerGlobalScope` from `webworker`
*/
declare class WorkerGlobalScope {}

/**
* Returns navigator.userAgent string or '' if it's not defined.
* @return user agent string
Expand Down Expand Up @@ -77,7 +82,18 @@ export function isNode(): boolean {
* Detect Browser Environment
*/
export function isBrowser(): boolean {
return typeof self === 'object' && self.self === self;
return typeof window !== 'undefined' || isWebWorker();
}

/**
* Detect Web Worker context
*/
export function isWebWorker(): boolean {
return (
typeof WorkerGlobalScope !== 'undefined' &&
typeof self !== 'undefined' &&
self instanceof WorkerGlobalScope
);
}

/**
Expand Down

0 comments on commit baa96ce

Please sign in to comment.