Skip to content

Commit

Permalink
fix: improve environment detection in is-browser utility (#1744)
Browse files Browse the repository at this point in the history
  • Loading branch information
hibrandonevans authored Nov 22, 2023
1 parent 4e77c77 commit b094142
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/lib/is-browser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const isBrowser =
(typeof window !== 'undefined' && typeof window.document !== 'undefined') ||
// eslint-disable-next-line no-restricted-globals
(typeof self === 'object' &&
// eslint-disable-next-line no-restricted-globals
self.constructor &&
const isStandardBrowserEnv = () =>
typeof window !== 'undefined' && typeof window.document !== 'undefined'

const isWebWorkerEnv = () =>
Boolean(
// eslint-disable-next-line no-restricted-globals
self.constructor.name === 'DedicatedWorkerGlobalScope') || // is web worker
(typeof navigator !== 'undefined' && navigator.product === 'ReactNative') // while navigator.product is deprecated
typeof self === 'object' &&
// eslint-disable-next-line no-restricted-globals
self?.constructor?.name?.includes('WorkerGlobalScope'),
)

const isReactNativeEnv = () =>
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'

const isBrowser =
isStandardBrowserEnv() || isWebWorkerEnv() || isReactNativeEnv()

export default isBrowser

0 comments on commit b094142

Please sign in to comment.