-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
responsive.ts
40 lines (32 loc) 路 955 Bytes
/
responsive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { headers } from 'next/headers';
import { UAParser } from 'ua-parser-js';
/**
* check mobile device in server
*/
export const isMobileDevice = () => {
if (typeof process === 'undefined') {
throw new Error('[Server method] you are importing a server-only module outside of server');
}
const { get } = headers();
const ua = get('user-agent');
// console.debug(ua);
const device = new UAParser(ua || '').getDevice();
return device.type === 'mobile';
};
/**
* check mobile device in server
*/
export const gerServerDeviceInfo = () => {
if (typeof process === 'undefined') {
throw new Error('[Server method] you are importing a server-only module outside of server');
}
const { get } = headers();
const ua = get('user-agent');
// console.debug(ua);
const parser = new UAParser(ua || '');
return {
browser: parser.getBrowser().name,
isMobile: isMobileDevice(),
os: parser.getOS().name,
};
};