-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Conversation
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? |
packages/ckeditor5-utils/src/env.js
Outdated
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() : ''; |
There was a problem hiding this comment.
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
toglobal
- version.js ->
const windowOrGlobal = global;
Probably changes are not required because when I revert it back, it was still working
There was a problem hiding this 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 👍
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
andtranslation-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.