Skip to content

Commit b094142

Browse files
fix: improve environment detection in is-browser utility (#1744)
1 parent 4e77c77 commit b094142

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/lib/is-browser.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
const isBrowser =
2-
(typeof window !== 'undefined' && typeof window.document !== 'undefined') ||
3-
// eslint-disable-next-line no-restricted-globals
4-
(typeof self === 'object' &&
5-
// eslint-disable-next-line no-restricted-globals
6-
self.constructor &&
1+
const isStandardBrowserEnv = () =>
2+
typeof window !== 'undefined' && typeof window.document !== 'undefined'
3+
4+
const isWebWorkerEnv = () =>
5+
Boolean(
76
// eslint-disable-next-line no-restricted-globals
8-
self.constructor.name === 'DedicatedWorkerGlobalScope') || // is web worker
9-
(typeof navigator !== 'undefined' && navigator.product === 'ReactNative') // while navigator.product is deprecated
7+
typeof self === 'object' &&
8+
// eslint-disable-next-line no-restricted-globals
9+
self?.constructor?.name?.includes('WorkerGlobalScope'),
10+
)
11+
12+
const isReactNativeEnv = () =>
13+
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
14+
15+
const isBrowser =
16+
isStandardBrowserEnv() || isWebWorkerEnv() || isReactNativeEnv()
1017

1118
export default isBrowser

0 commit comments

Comments
 (0)