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

Handle call to navigator API for envs without navigator API. #11999

Merged
merged 5 commits into from
Aug 23, 2022

Conversation

CatStrategist
Copy link
Contributor

@CatStrategist CatStrategist commented Jun 30, 2022

Suggested merge commit message (convention)

Internal (utils): Wrapped accessing browser's API in try/catch blocks to safely initialize editor in environments without browser's APIs.


Additional information

env.ts, global.ts and translation-service.ts files might be imported in environments without browsers' APIs - like Node.
The best solution would be to get it out of a global scope to a function call, so it's not executed on the file import, but that would require a change in fifty files wherever it's imported.

@Reinmar
Copy link
Member

Reinmar commented Jul 13, 2022

This change requires tests. How do we know that it actually doesn't crash later on when the string is empty?

Also, perhaps it'd be better if a "no browser" tried to mimick some known browser? Chrome for instance as it's most popular?

const userAgent = navigator.userAgent.toLowerCase();
// This file might be imported in environments without the navigator API.
/* istanbul ignore next */
const userAgent = navigator ? navigator.userAgent.toLowerCase() : '';
Copy link

@dpawlowski-cksource dpawlowski-cksource Jul 19, 2022

Choose a reason for hiding this comment

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

It doesn't work. Still, the result is:

ReferenceError: navigator is not defined

When I replaced this line to const userAgent = ''; then it went on, but threw another error:

ReferenceError: window is not defined

When I fixed all places it started to work 🎉

Places that I have changed:

  • global.js -> export default { window: {}, document: {} };
  • env.js -> const userAgent = '';
  • translation-service.js -> replaced window to global
  • version.js -> const windowOrGlobal = global; Probably changes are not required because when I revert it back, it was still working

Copy link

@dpawlowski-cksource dpawlowski-cksource left a comment

Choose a reason for hiding this comment

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

Tested those changes manually in Node.js and looks like it works 👍

@arkflpc arkflpc merged commit 64c47c7 into master Aug 23, 2022
@arkflpc arkflpc deleted the cf/4611-handle-no-navigator-api branch August 23, 2022 13:28
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.

4 participants