Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Fix browser detection
Web Workers are browser too.
  • Loading branch information
mmermerkaya committed May 9, 2019
commit 4789457558e50bfc36a55ad331ee76a8e35886f3
2 changes: 1 addition & 1 deletion packages/util/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ 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.

}

/**
Expand Down