Skip to content

Fix browser detection #1773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions packages/util/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { CONSTANTS } from './constants';

/**
* Returns navigator.userAgent string or '' if it's not defined.
* @return {string} user agent string
* @return user agent string
*/
export function getUA(): string {
if (
Expand All @@ -35,10 +35,9 @@ export function getUA(): string {
/**
* Detect Cordova / PhoneGap / Ionic frameworks on a mobile device.
*
* Deliberately does not rely on checking `file://` URLs (as this fails PhoneGap in the Ripple emulator) nor
* Cordova `onDeviceReady`, which would normally wait for a callback.
*
* @return {boolean} isMobileCordova
* Deliberately does not rely on checking `file://` URLs (as this fails PhoneGap
* in the Ripple emulator) nor Cordova `onDeviceReady`, which would normally
* wait for a callback.
*/
export function isMobileCordova(): boolean {
return (
Expand All @@ -51,9 +50,9 @@ export function isMobileCordova(): boolean {
/**
* Detect Node.js.
*
* @return {boolean} True if Node.js environment is detected.
* Node detection logic from: https://github.com/iliakan/detect-node/
* @return true if Node.js environment is detected.
*/
// Node detection logic from: https://github.com/iliakan/detect-node/
export function isNode(): boolean {
try {
return (
Expand All @@ -68,13 +67,13 @@ export function isNode(): boolean {
* Detect Browser Environment
*/
export function isBrowser(): boolean {
return typeof window !== 'undefined';
return typeof self === 'object' && self.self === self;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why checking self.self? Does some none browser env have self, but not self.self, or just to handle the case where someone defined a variable named self in none browse env?

The intention of this function was just to check if it's a browser window env.
My intuition is that webworker probably needs a dedicated env check logic, but we don't need it anywhere. Do you see any implication of treating webworker as a browser env?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make sure that self is actually the global self, and not some random global that a developer defined.

I thought that in the place you use this function, you'd want the result to be true for web workers as well. It's a useful check in both window and web worker contexts.

}

/**
* Detect React Native.
*
* @return {boolean} True if ReactNative environment is detected.
* @return true if ReactNative environment is detected.
*/
export function isReactNative(): boolean {
return (
Expand All @@ -85,7 +84,7 @@ export function isReactNative(): boolean {
/**
* Detect whether the current SDK build is the Node version.
*
* @return {boolean} True if it's the Node SDK build.
* @return true if it's the Node SDK build.
*/
export function isNodeSdk(): boolean {
return CONSTANTS.NODE_CLIENT === true || CONSTANTS.NODE_ADMIN === true;
Expand Down