Skip to content

Commit 5f7c0d0

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

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

.changeset/chilly-moons-play.md

Lines changed: 5 additions & 0 deletions
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)

config/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"lib": [
88
"es5",
99
"dom",
10+
"WebWorker",
1011
"es2015.promise",
1112
"es2015.symbol",
1213
"es2015.iterable",

packages/util/src/environment.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,17 @@ export function isNode(): boolean {
7777
* Detect Browser Environment
7878
*/
7979
export function isBrowser(): boolean {
80-
return typeof self === 'object' && self.self === self;
80+
return typeof window !== 'undefined' || isWebWorker();
81+
}
82+
83+
/**
84+
* Detect Web Worker context
85+
*/
86+
export function isWebWorker(): boolean {
87+
return (
88+
typeof WorkerGlobalScope !== 'undefined' &&
89+
self instanceof WorkerGlobalScope
90+
);
8191
}
8292

8393
/**

0 commit comments

Comments
 (0)