-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
enhancement: better detect apple silicon #8330
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,37 @@ type UserOSState = { | |
| architecture: Architecture | ''; | ||
| }; | ||
|
|
||
| // taken from https://github.com/faisalman/ua-parser-js/issues/732#issue-2348848266 | ||
| function isAppleSilicon() { | ||
| try { | ||
| // Best guess if the device uses Apple Silicon: https://stackoverflow.com/a/65412357 | ||
| const webglContext = document.createElement('canvas').getContext('webgl'); | ||
| if (webglContext == null) { | ||
| return false; | ||
| } | ||
| const debugInfo = webglContext.getExtension('WEBGL_debug_renderer_info'); | ||
| const renderer = | ||
| (debugInfo && | ||
| webglContext.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL)) || | ||
| ''; | ||
| if (renderer.match(/Apple/) && !renderer.match(/Apple GPU/)) { | ||
| return true; | ||
| } | ||
|
|
||
| if ( | ||
| webglContext | ||
| .getSupportedExtensions() | ||
| ?.includes('WEBGL_compressed_texture_s3tc_srgb') | ||
| ) { | ||
| return true; | ||
| } | ||
| } catch { | ||
| /**/ | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| const useDetectOS = () => { | ||
| const [userOSState, setUserOSState] = useState<UserOSState>({ | ||
| os: 'LOADING', | ||
|
|
@@ -41,7 +72,7 @@ const useDetectOS = () => { | |
| // If there is no getHighEntropyValues API on the Browser or it failed to resolve | ||
| // we attempt to fallback to what the User Agent indicates | ||
| bitness = uaIndicates64 ? '64' : '32', | ||
| architecture = 'x86', | ||
| architecture = isAppleSilicon() ? 'arm' : 'x86', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of "platform specific" workarounds. + We are literally creating an element on the page, that is breaking the "React" bubble. I'm not a fan of using API's outside of React (window/globals/etc) and we usually only use with Hooks. If you see the only |
||
| }) => { | ||
| setUserOSState(current => ({ | ||
| ...current, | ||
|
|
||
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.
Also, this approach isn't the one the authors of that library ended up going with
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.
sure, i can pull in that approach if it's better.
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.
more importantly, ua-parser-js is AGPL, and as far as I know the strong copy-left would prohibit MIT usage
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.
When that is the only issue left, I'll reach out and see if I can get written permission to consider the snippet MIT.