Repository navigation
Expand file tree
/
Copy pathbrowser.ts
More file actions
141 lines (132 loc) · 4.25 KB
/
Copy pathbrowser.ts
File metadata and controls
141 lines (132 loc) · 4.25 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Checks if the window object is defined. You can also use this to check if something is happening on the client side.
*
* @returns
*/
export function inBrowser(): boolean {
return typeof window !== 'undefined';
}
const botAgents = [
'bot',
'spider',
'crawl',
'APIs-Google',
'AdsBot',
'Googlebot',
'mediapartners',
'Google Favicon',
'FeedFetcher',
'Google-Read-Aloud',
'DuplexWeb-Google',
'googleweblight',
'bing',
'yandex',
'baidu',
'duckduck',
'yahoo',
'ecosia',
'ia_archiver',
'facebook',
'instagram',
'pinterest',
'reddit',
'slack',
'twitter',
'whatsapp',
'youtube',
'semrush',
];
const botAgentRegex = new RegExp(botAgents.join('|'), 'i');
/**
* Checks if the user agent is a bot.
*
* @param userAgent - Any user agent string
* @returns
*/
export function userAgentIsRobot(userAgent: string): boolean {
return !userAgent ? false : botAgentRegex.test(userAgent);
}
/**
* Server-side runtimes with worker-like globals self-identify in `navigator.userAgent`
* (`Cloudflare-Workers`, `Node.js/24`, `Deno/2.5.0`, `Bun/1.3.9`). Today workerd's `self`
* does not satisfy `instanceof WorkerGlobalScope` (even though it exposes the constructor),
* so the scope gate alone happens to exclude it, but that is an implementation detail of
* workerd's prototype chain, not a guarantee. Excluding self-identified server runtimes by
* user agent keeps these heuristics server-false even if such a runtime becomes fully
* spec-compliant about its worker scope.
*/
const serverRuntimeUserAgentRegex = /^(Cloudflare-Workers|Node\.js|Deno|Bun)\b/i;
/**
* Resolves the `Navigator` object from either the DOM `window` (standard browsers)
* or a Web/Service Worker global scope. An MV3 extension background service worker
* has no `window`, but runs inside a `WorkerGlobalScope` that exposes a
* `WorkerNavigator` as `self.navigator` with the `onLine`/`userAgent` properties
* our heuristics rely on.
*
* We intentionally gate the worker fallback on a real `WorkerGlobalScope` rather than
* accepting any global `navigator`. Modern Node exposes `globalThis.navigator`, so a
* blanket global-navigator check would make Node SSR look like a browser; requiring a
* `WorkerGlobalScope` keeps SSR returning `null`.
*
* @returns
*/
function getNavigator(): Navigator | null {
if (typeof window !== 'undefined' && window.navigator) {
return window.navigator;
}
const workerScope = globalThis as unknown as {
WorkerGlobalScope?: new (...args: never[]) => { navigator?: Navigator };
self?: { navigator?: Navigator };
};
if (
typeof workerScope.WorkerGlobalScope === 'function' &&
workerScope.self instanceof workerScope.WorkerGlobalScope &&
workerScope.self.navigator &&
!serverRuntimeUserAgentRegex.test(workerScope.self.navigator.userAgent ?? '')
) {
return workerScope.self.navigator;
}
return null;
}
/**
* Checks if the current environment is a browser and the user agent is not a bot.
*
* @returns
*/
export function isValidBrowser(): boolean {
const navigator = getNavigator();
if (!navigator) {
return false;
}
return !userAgentIsRobot(navigator?.userAgent) && !navigator?.webdriver;
}
/**
* Checks if the current environment is a browser and if the navigator is online.
*
* @returns
*/
export function isBrowserOnline(): boolean {
const navigator = getNavigator();
if (!navigator) {
return false;
}
// Some environments (e.g. React Native) define a Navigator object but do not
// implement navigator.onLine as a boolean. Default to online in those cases.
if (typeof navigator.onLine !== 'boolean') {
return true;
}
// navigator.onLine is the standard API and is reliable for detecting
// complete disconnection (airplane mode, WiFi off, etc.).
// The experimental navigator.connection API (rtt/downlink) was previously
// used as a secondary signal, but it reports zero values in headless browsers
// and CI environments even when connected, causing false offline detection.
return !!navigator.onLine;
}
/**
* Runs `isBrowserOnline` and `isValidBrowser` to check if the current environment is a valid browser and if the navigator is online.
*
* @returns
*/
export function isValidBrowserOnline(): boolean {
return isBrowserOnline() && isValidBrowser();
}