Skip to content
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

Fix browser detection #1773

Merged
merged 2 commits into from
May 10, 2019
Merged

Fix browser detection #1773

merged 2 commits into from
May 10, 2019

Conversation

mmermerkaya
Copy link
Contributor

Web Workers are browser too.

@mmermerkaya
Copy link
Contributor Author

BTW, should we merge these isX functions into a single detectEnvironment function?

Web Workers are browser too.
@@ -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.

@Feiyang1
Copy link
Member

Feiyang1 commented May 9, 2019

I was thinking something along that line too. We could just export the result of the function. Then you can do something like

import { Environment, ENVIRONMENT } from `@firebase/util`
if (Environment === ENVIRONMENT.Browser) ....

We can also extend the function to return an object if there is other useful information. I think it's a good idea.

@mmermerkaya mmermerkaya merged commit 46ba69a into master May 10, 2019
@mmermerkaya mmermerkaya deleted the mmermerkaya-fix-isbrowser branch May 10, 2019 18:54
@firebase firebase locked and limited conversation to collaborators Oct 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants