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

Protect navigator access from causing a ReferenceError #323

Merged
merged 1 commit into from
Aug 30, 2023

Conversation

kungfooman
Copy link
Contributor

Fixes: #322

Now we can properly import it in node via await import('./dist/module/src/index.mjs') aswell:

image

@@ -8,7 +8,7 @@ const CLASS_SLIDER_BAR = CLASS_SLIDER + '-bar';
const CLASS_SLIDER_HANDLE = CLASS_SLIDER + '-handle';
const CLASS_SLIDER_ACTIVE = CLASS_SLIDER + '-active';

const IS_CHROME = /Chrome\//.test(navigator.userAgent);
const IS_CHROME = /Chrome\//.test(globalThis.navigator?.userAgent);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const IS_CHROME = /Chrome\//.test(globalThis.navigator?.userAgent);
const IS_CHROME = /Chrome\//.test(navigator?.userAgent || "");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't work like that in node:

(base) homepc@home-pc:~$ node
Welcome to Node.js v18.12.1.
Type ".help" for more information.
> IS_CHROME_A = /Chrome\//.test(navigator?.userAgent || "");
Uncaught ReferenceError: navigator is not defined
> IS_CHROME_B = /Chrome\//.test(globalThis.navigator?.userAgent);
false
> 

And in browsers it works aswell:

image

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, yeah, I see. So I guess I was just trying to eliminate globalThis because I was concerned that it's a reasonably new addition to JS (ES2020). In the engine for example, we don't use globalThis anywhere except in the JSDoc comments. Instead, we tend to do this:

https://github.com/playcanvas/engine/blob/26a73a1fb6db893936c446a43611ff649290a27c/src/core/platform.js#L19-L20

I don't think Babel will transpile globalThis for older browsers.

All this said, this is PCUI and not the engine so it's much less likely to run in older browsers. I'm also wondering whether the IS_CHROME test is even still needed since it seems to be being used to get around some old browser problem that might not even be an issue today.

I'll approve + merge for now though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good points, if we run into issues, we could replace it with:

> IS_CHROME_C = typeof navigator !== 'undefined' ? /Chrome\//.test(navigator.userAgent) : false;
false

Or test if we still require this, as you said 👍

@willeastcott willeastcott merged commit 91cc6d2 into playcanvas:main Aug 30, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PCUI isn't importable in ESM/node - Uncaught ReferenceError: navigator is not defined
2 participants