Skip to content

Commit

Permalink
fix(electron): detect electron context (#1856)
Browse files Browse the repository at this point in the history
* fix(electron): detect electron env

* fix(electron): cleanup code

* fix: fixed wrong operator

* fix(electron): improved code and add some comments

* Update src/lib/is-browser.ts

Co-authored-by: Daniel Lando <daniel.sorridi@gmail.com>

* fix: typo and lint

---------

Co-authored-by: Daniel Lando <daniel.sorridi@gmail.com>
  • Loading branch information
axi92 and robertsLando committed May 6, 2024
1 parent c6580a6 commit 6a03d29
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/lib/is-browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
const isStandardBrowserEnv = () =>
typeof window !== 'undefined' && typeof window.document !== 'undefined'
const isStandardBrowserEnv = () => {
// window is only defined when it is a browser
if (typeof window !== 'undefined') {
// Is the process an electron application
// check if we are in electron `renderer`
const electronRenderCheck =
navigator?.userAgent?.toLowerCase().indexOf(' electron/') > -1
if (electronRenderCheck && process?.versions) {
const electronMainCheck = Object.prototype.hasOwnProperty.call(
process.versions,
'electron',
)
// Both electron checks are only true if the following webPreferences are set in the main electron BrowserWindow()
// webPreferences: {
// sandbox: false,
// nodeIntegration: true
// contextIsolation: false
// }
return !electronMainCheck
}
return typeof window.document !== 'undefined'
}
// return false if nothing is detected
return false
}

const isWebWorkerEnv = () =>
Boolean(
Expand Down

5 comments on commit 6a03d29

@cmoniz-ocean
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to break electron 12.2.3 even when adding the webPreferences changes:
Screenshot 2024-05-16 at 9 46 32 AM
Screenshot 2024-05-16 at 9 46 43 AM
Screenshot 2024-05-16 at 9 53 53 AM

@robertsLando
Copy link
Member

Choose a reason for hiding this comment

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

@axi92 Want to sbumit a pr to fix the issue?

@axi92
Copy link
Contributor Author

@axi92 axi92 commented on 6a03d29 May 17, 2024

Choose a reason for hiding this comment

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

Electron is currently on version 30.x.x
Version 12 reached EOL 2021 and was on node 14.
I can try but I don't think I can make a fix for all electron versions.

@cmoniz-ocean Are you maybe able to upgrade your electron version to a maintained version? You can check the timelines here https://www.electronjs.org/docs/latest/tutorial/electron-timelines

@cmoniz-ocean
Copy link
Contributor

@cmoniz-ocean cmoniz-ocean commented on 6a03d29 May 17, 2024

Choose a reason for hiding this comment

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

@axi92 I was able to upgrade to electron 30, but I'll be validating hardware compatibility tomorrow

(I think a typeof somewhere here might fix the bug - #1868)

@cmoniz-ocean
Copy link
Contributor

Choose a reason for hiding this comment

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

@axi92 Apparently 5.6.0 throws this error with electron version 30.0.6 as well - I've added a PR #1868

Please sign in to comment.