Skip to content

Commit c6ed3d5

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

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ 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 w = document.createElement('canvas').getContext('webgl');
23+
if (w == null) {
24+
return false;
25+
}
26+
const d = w.getExtension('WEBGL_debug_renderer_info');
27+
const g = (d && w.getParameter(d.UNMASKED_RENDERER_WEBGL)) || '';
28+
if (g.match(/Apple/) && !g.match(/Apple GPU/)) {
29+
return true;
30+
}
31+
32+
if (
33+
w.getSupportedExtensions()?.includes('WEBGL_compressed_texture_s3tc_srgb')
34+
) {
35+
return true;
36+
}
37+
} catch {
38+
/**/
39+
}
40+
41+
return false;
42+
}
43+
1844
const useDetectOS = () => {
1945
const [userOSState, setUserOSState] = useState<UserOSState>({
2046
os: 'LOADING',
@@ -41,7 +67,7 @@ const useDetectOS = () => {
4167
// If there is no getHighEntropyValues API on the Browser or it failed to resolve
4268
// we attempt to fallback to what the User Agent indicates
4369
bitness = uaIndicates64 ? '64' : '32',
44-
architecture = 'x86',
70+
architecture = isAppleSilicon() ? 'arm64' : 'x86',
4571
}) => {
4672
setUserOSState(current => ({
4773
...current,

0 commit comments

Comments
 (0)