Skip to content

Commit f51a179

Browse files
committed
enhancement: better detect apple silicon
1 parent 30d6c52 commit f51a179

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

apps/site/hooks/react-client/useDetectOS.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,37 @@ type UserOSState = {
1515
architecture: Architecture | '';
1616
};
1717

18+
// taken from https://github.com/faisalman/ua-parser-js/issues/732#issue-2348848266
19+
function isAppleSilicon() {
20+
try {
21+
// Best guess if the device uses Apple Silicon: https://stackoverflow.com/a/65412357
22+
const webglContext = document.createElement('canvas').getContext('webgl');
23+
if (webglContext == null) {
24+
return false;
25+
}
26+
const debugInfo = webglContext.getExtension('WEBGL_debug_renderer_info');
27+
const renderer =
28+
(debugInfo &&
29+
webglContext.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL)) ||
30+
'';
31+
if (renderer.match(/Apple/) && !renderer.match(/Apple GPU/)) {
32+
return true;
33+
}
34+
35+
if (
36+
webglContext
37+
.getSupportedExtensions()
38+
?.includes('WEBGL_compressed_texture_s3tc_srgb')
39+
) {
40+
return true;
41+
}
42+
} catch {
43+
/**/
44+
}
45+
46+
return false;
47+
}
48+
1849
const useDetectOS = () => {
1950
const [userOSState, setUserOSState] = useState<UserOSState>({
2051
os: 'LOADING',
@@ -41,7 +72,7 @@ const useDetectOS = () => {
4172
// If there is no getHighEntropyValues API on the Browser or it failed to resolve
4273
// we attempt to fallback to what the User Agent indicates
4374
bitness = uaIndicates64 ? '64' : '32',
44-
architecture = 'x86',
75+
architecture = isAppleSilicon() ? 'arm' : 'x86',
4576
}) => {
4677
setUserOSState(current => ({
4778
...current,

0 commit comments

Comments
 (0)