Skip to content

Commit baa96ce

Browse files
committed
fix: browser detection
update browser detection logic: detect either window or web worker (WorkerGlobalScope). fixes firebase#8299 firebase#8284
1 parent a90255a commit baa96ce

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

.changeset/chilly-moons-play.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/util': patch
3+
---
4+
5+
fix: browser detection (detect either window or web worker)

packages/util/src/environment.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
import { CONSTANTS } from './constants';
1919
import { getDefaults } from './defaults';
2020

21+
/**
22+
* Type placeholder for `WorkerGlobalScope` from `webworker`
23+
*/
24+
declare class WorkerGlobalScope {}
25+
2126
/**
2227
* Returns navigator.userAgent string or '' if it's not defined.
2328
* @return user agent string
@@ -77,7 +82,18 @@ export function isNode(): boolean {
7782
* Detect Browser Environment
7883
*/
7984
export function isBrowser(): boolean {
80-
return typeof self === 'object' && self.self === self;
85+
return typeof window !== 'undefined' || isWebWorker();
86+
}
87+
88+
/**
89+
* Detect Web Worker context
90+
*/
91+
export function isWebWorker(): boolean {
92+
return (
93+
typeof WorkerGlobalScope !== 'undefined' &&
94+
typeof self !== 'undefined' &&
95+
self instanceof WorkerGlobalScope
96+
);
8197
}
8298

8399
/**

0 commit comments

Comments
 (0)