From baa96ce400eba597600446915abef7ffd22736ed Mon Sep 17 00:00:00 2001 From: Jose V Sebastian Date: Wed, 26 Jun 2024 08:12:22 +0530 Subject: [PATCH] fix: browser detection update browser detection logic: detect either window or web worker (WorkerGlobalScope). fixes #8299 #8284 --- .changeset/chilly-moons-play.md | 5 +++++ packages/util/src/environment.ts | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .changeset/chilly-moons-play.md diff --git a/.changeset/chilly-moons-play.md b/.changeset/chilly-moons-play.md new file mode 100644 index 00000000000..1d1db3c15c6 --- /dev/null +++ b/.changeset/chilly-moons-play.md @@ -0,0 +1,5 @@ +--- +'@firebase/util': patch +--- + +fix: browser detection (detect either window or web worker) diff --git a/packages/util/src/environment.ts b/packages/util/src/environment.ts index f2afaede0f1..30fc661bfc6 100644 --- a/packages/util/src/environment.ts +++ b/packages/util/src/environment.ts @@ -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 @@ -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 + ); } /**