Skip to content

Commit 0b852c3

Browse files
Editorial review: Information about User-Agent reduction (#41648)
* Add information to MDN about User-Agent reduction * Add information to MDN about User-Agent reduction * Fixes for miketaylr review comments * remove bit that doesnt really exist * Fixes for hamish review comments * A few more bits of clean-up
1 parent 6aa1d0e commit 0b852c3

File tree

7 files changed

+319
-95
lines changed

7 files changed

+319
-95
lines changed

files/en-us/web/api/navigator/appversion/index.md

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,40 @@ browser-compat: api.Navigator.appVersion
88

99
{{APIRef("HTML DOM")}}
1010

11-
Returns either `"4.0"` or a string representing version information about
12-
the browser.
13-
14-
> [!NOTE]
15-
> Do not rely on this property to return the correct browser version.
11+
The **`Navigator.appVersion`** read-only property of the {{domxref("Navigator")}} interface returns a string representing version information about the browser.
1612

1713
## Value
1814

19-
Either `"4.0"` or a string representing version information about the
20-
browser.
15+
A string.
2116

22-
## Examples
17+
## Description
2318

24-
```js
25-
alert(`Your browser version is reported as ${navigator.appVersion}`);
19+
The `appVersion` property returns information indicating the browser version.
20+
21+
Note that the information returned varies significantly by browser. In some browsers, such as Chrome, this is nearly the same as the value returned by {{domxref("Navigator.userAgent")}}, with the `Mozilla/` prefix removed. For example:
22+
23+
```plain
24+
5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
2625
```
2726

28-
## Notes
27+
In other browsers, such as Firefox, this is cut down to a short string that hints at the platform/OS. For example:
28+
29+
```plain
30+
5.0 (Macintosh)
31+
```
2932

30-
The `window.navigator.userAgent` property may also contain the version
31-
number (for example
32-
`"Mozilla/5.0 (Windows; U; Win98; en-US; rv:0.9.2) Gecko/20010725 Netscape 6/6.1"`),
33-
but you should be aware of how easy it is to change the user agent string and "spoof"
34-
other browsers, platforms, or user agents, and also how cavalier the browser vendor
35-
themselves are with these properties.
33+
Theoretically this information is useful for detecting the browser and serving code to work around browser-specific bugs or lack of feature support. However, this is **unreliable** and **is not recommended** for the reasons given in [User-Agent reduction](/en-US/docs/Web/HTTP/Guides/User-agent_reduction) and [Browser detection using the user agent](/en-US/docs/Web/HTTP/Guides/Browser_detection_using_the_user_agent).
3634

37-
The `window.navigator.appVersion`, `window.navigator.appName` and
38-
`window.navigator.userAgent` properties have been used in "browser sniffing"
39-
code: scripts that attempt to find out what kind of browser you are using and adjust
40-
pages accordingly. This lead to the current situation, where browsers had to return fake
41-
values from these properties in order not to be locked out of some websites.
35+
[Feature detection](/en-US/docs/Learn_web_development/Extensions/Testing/Feature_detection) is a much more reliable strategy.
36+
37+
## Examples
38+
39+
```js
40+
console.log(navigator.appVersion);
41+
// On Chrome, logs something like "5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36" (reduced UA string)
42+
43+
// On Firefox, logs something like "5.0 (Macintosh)"
44+
```
4245

4346
## Specifications
4447

@@ -47,3 +50,8 @@ values from these properties in order not to be locked out of some websites.
4750
## Browser compatibility
4851

4952
{{Compat}}
53+
54+
## See also
55+
56+
- {{domxref("Navigator.userAgent")}}
57+
- {{HTTPHeader("User-agent")}} HTTP header

files/en-us/web/api/navigator/platform/index.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,39 @@ browser-compat: api.Navigator.platform
1010

1111
The **`platform`** property read-only property of the {{domxref("Navigator")}} interface returns a string identifying the platform on which the user's browser is running.
1212

13+
## Value
14+
15+
A string indicating a platform, for example:
16+
17+
- `"MacIntel"`
18+
- `"Win32"`
19+
- `"Linux x86_64"`
20+
1321
> [!NOTE]
14-
> In general, you should whenever possible avoid writing code that uses methods or properties like this one to try to find out information about the user's environment, and instead write code that does [feature detection](/en-US/docs/Learn_web_development/Extensions/Testing/Feature_detection).
22+
> On Windows, modern browsers return `"Win32"` even if running on a 64-bit version of Windows.
1523
16-
## Value
24+
## Description
1725

18-
A string identifying the platform on which the user's browser is running; for example: `"MacIntel"`, `"Win32"`, `"Linux x86_64"`, `"Linux armv81"`.
26+
The `platform` property indicates the platform/OS the browser is running on.
27+
28+
Theoretically this information is useful for detecting the browser and serving code to work around browser-specific bugs or lack of feature support. However, this is **unreliable** and **is not recommended** for the reasons given in [User-Agent reduction](/en-US/docs/Web/HTTP/Guides/User-agent_reduction) and [Browser detection using the user agent](/en-US/docs/Web/HTTP/Guides/Browser_detection_using_the_user_agent).
29+
30+
[Feature detection](/en-US/docs/Learn_web_development/Extensions/Testing/Feature_detection) is a much more reliable strategy.
1931

2032
## Examples
2133

22-
`navigator.platform` should almost always be avoided in favor of [feature detection](/en-US/docs/Learn_web_development/Extensions/Testing/Feature_detection). But there is one case where, among the options you could use, `navigator.platform` may be the least-bad option: When you need to show users advice about whether the modifier key for keyboard shortcuts is the `` command key (found on Apple systems) rather than the `` control key (on non-Apple systems):
34+
### Determining the modifier key for the user's platform
35+
36+
One case where `navigator.platform` can be useful is when you need to show users advice about whether the modifier key for keyboard shortcuts is the `` command key (found on Apple systems) rather than the `Ctrl` control key (on non-Apple systems):
2337

2438
```js
2539
const modifierKeyPrefix =
2640
navigator.platform.startsWith("Mac") || navigator.platform === "iPhone"
2741
? "" // command key
28-
: "^"; // control key
42+
: "Ctrl"; // control key
2943
```
3044

31-
That is, check if `navigator.platform` starts with `"Mac"` or else is an exact match for `"iPhone"`, and then based on whether either of those is true, choose the modifier key your web application's UI will advise users to press in keyboard shortcuts.
32-
33-
## Usage notes
34-
35-
On Windows, modern browsers return `"Win32"` even if running on a 64-bit version of Windows.
45+
This code checks if `navigator.platform` starts with `"Mac"` or else is an exact match for `"iPhone"`, and then based on whether either of those is `true`, sets a `modifierKeyPrefix` variable to the appropriate modifier key for the user's platform. This could be used in a web UI to tell users which modifier key they need when using keyboard shortcuts.
3646

3747
## Specifications
3848

@@ -44,4 +54,5 @@ On Windows, modern browsers return `"Win32"` even if running on a 64-bit version
4454

4555
## See also
4656

47-
- [`navigator.userAgentData.platform`](/en-US/docs/Web/API/NavigatorUAData/platform)
57+
- {{domxref("Navigator.userAgent")}}
58+
- {{HTTPHeader("User-agent")}} HTTP header

files/en-us/web/api/navigator/useragent/index.md

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,29 @@ browser-compat: api.Navigator.userAgent
88

99
{{ApiRef("HTML DOM")}}
1010

11-
The **`Navigator.userAgent`** read-only property returns the
12-
user agent string for the current browser.
13-
14-
> [!NOTE]
15-
> The specification asks browsers to provide as little information via this field as
16-
> possible. Never assume that the value of this property will stay the same in future
17-
> versions of the same browser. Try not to use it at all, or only for current and past
18-
> versions of a browser. New browsers may start using the same UA, or part of it, as an
19-
> older browser: you really have no guarantee that the browser agent is indeed the one
20-
> advertised by this property.
21-
>
22-
> Also keep in mind that users of a browser can change the value of this field if they
23-
> want (UA spoofing).
24-
25-
Browser identification based on detecting the user agent string is
26-
**unreliable** and **is not recommended**, as the user agent
27-
string is user configurable. For example:
28-
29-
- In Firefox, you can change the preference `general.useragent.override` in
30-
`about:config`. Some Firefox extensions do that; however, this only changes
31-
the HTTP header that gets sent and that is returned by `navigator.userAgent`.
32-
There might be other methods that utilize JavaScript code to identify the browser.
33-
- Opera 6+ allows users to set the browser identification string via a menu.
11+
The **`Navigator.userAgent`** read-only property of the {{domxref("Navigator")}} interface returns the `User-Agent` (UA) string for the current browser.
3412

3513
## Value
3614

37-
A string specifying the complete user agent string the browser
38-
provides both in {{Glossary("HTTP")}} headers and in response to this and other related
39-
methods on the {{domxref("Navigator")}} object.
15+
A string.
4016

41-
The user agent string is built on a formal structure which can be decomposed into
42-
several pieces of info. Each of these pieces of info comes from other navigator
43-
properties which are also settable by the user. For more information about the
44-
form of the user agent string, see the {{HTTPHeader("User-agent")}} HTTP header.
17+
## Description
18+
19+
The `userAgent` property provides the current browser's UA string. The UA string is built on a formal structure, which can be decomposed into several pieces of information.
20+
21+
The browser also provides the UA string via the {{HTTPHeader("User-Agent")}} HTTP header. Parts of this information are also available in {{Glossary("HTTP")}} headers such as [User-Agent client hints](/en-US/docs/Web/HTTP/Guides/Client_hints) and other related API features such as {{domxref("Navigator.appVersion")}} and {{domxref("Navigator.platform")}}.
22+
23+
Theoretically this information is useful for detecting the browser and serving code to work around browser-specific bugs or lack of feature support. However, this is **unreliable** and **is not recommended** for the reasons given in [User-Agent reduction](/en-US/docs/Web/HTTP/Guides/User-agent_reduction) and [Browser detection using the user agent](/en-US/docs/Web/HTTP/Guides/Browser_detection_using_the_user_agent).
24+
25+
[Feature detection](/en-US/docs/Learn_web_development/Extensions/Testing/Feature_detection) is a much more reliable strategy.
4526

4627
## Examples
4728

4829
```js
49-
alert(window.navigator.userAgent);
50-
// alerts "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0"
30+
console.log(navigator.userAgent);
31+
// On Chrome on macOS, logs something like "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36" (reduced UA string)
32+
33+
// On Firefox on Windows, logs something like "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0"
5134
```
5235

5336
## Specifications

files/en-us/web/http/guides/client_hints/index.md

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,41 @@ The set of "hint" headers are listed in the topic [HTTP Headers](/en-US/docs/Web
1313

1414
## Overview
1515

16-
A server must announce that it supports client hints, using the {{HTTPHeader("Accept-CH")}} header to specify the hints that it is interested in receiving.
17-
When a client that supports client hints receives the `Accept-CH` header it can choose to append some or all of the listed client hint headers in its subsequent requests.
16+
1. When the browser first makes a request to load a webpage, it will send the {{httpheader("User-Agent")}} header to the server.
17+
2. Additionally, it will send the server a default set of `Sec-CH-UA-*` headers; this set of hints are referred to as the [low entropy hints](#low_entropy_hints). An Android device, for example, would send something like this:
1818

19-
For example, following `Accept-CH` in a response below, the client could append {{HTTPHeader("Width")}}, {{HTTPHeader("Downlink")}} and {{HTTPHeader("Sec-CH-UA")}} headers to all subsequent requests.
19+
```http
20+
Sec-CH-UA: "Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"
21+
Sec-CH-UA-Platform: "Android"
22+
Sec-CH-UA-Mobile: ?1
23+
```
2024

21-
```http
22-
Accept-CH: Width, Downlink, Sec-CH-UA
23-
```
25+
These headers provide the following information:
26+
- {{httpheader("Sec-CH-UA")}}: The major browser version and other brands associated with it.
27+
- {{httpheader("Sec-CH-UA-Platform")}}: The platform.
28+
- {{httpheader("Sec-CH-UA-Mobile")}}: A boolean that indicates whether the browser is running on a mobile device (`?1`) or not (`?0`).
29+
30+
3. The server can announce that it supports client hints and request additional client hints using the {{httpheader("Accept-CH")}} response header, which contains a comma-delimited list of the additional headers it would like to receive in subsequent requests. For example:
31+
32+
```http
33+
Accept-CH: Sec-CH-UA-Model, Sec-CH-UA-Form-Factors
34+
```
35+
36+
The default set of headers is always sent; in this case, we've also requested:
37+
- {{httpheader("Sec-CH-UA-Model")}}: The device model the platform is running on.
38+
- {{httpheader("Sec-CH-UA-Form-Factors")}}: The device's form factor(s), which indicate how the user interacts with the user-agent — the screen size, controls, etc.
2439

25-
This approach is efficient in that the server only requests the information that it is able to usefully handle.
26-
It is also relatively "privacy-preserving", in that it is up to the client to decide what information it can safely share.
40+
4. If the browser is permitted, it will send the requested headers in all subsequent requests, until the browser or tab is closed. For example, our example Android phone might send the following updated headers with subsequent requests:
2741

28-
There is a small set of [low entropy client hint headers](#low_entropy_hints) that may be sent by a client even if not requested.
42+
```http
43+
Sec-CH-UA: "Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"
44+
Sec-CH-UA-Platform: "Android"
45+
Sec-CH-UA-Mobile: ?1
46+
Sec-CH-UA-Model: "Pixel 9"
47+
Sec-CH-UA-Form-Factors: "Mobile"
48+
```
49+
50+
This approach is efficient in that the server only requests the information that it is able to usefully handle. It is also relatively "privacy-preserving", in that it is up to the client to decide what information it can safely share.
2951

3052
> [!NOTE]
3153
> Client hints can also be specified in HTML using the {{HTMLElement("meta")}} element with the [`http-equiv`](/en-US/docs/Web/HTML/Reference/Elements/meta/http-equiv) attribute.
@@ -63,8 +85,8 @@ For example, to stop requesting any hints it would send `Accept-CH` with an empt
6385
## Low entropy hints
6486

6587
Client hints are broadly divided into high and low entropy hints.
66-
The low entropy hints are those that don't give away much information that might be used to create a [fingerprinting](/en-US/docs/Glossary/Fingerprinting) for a user.
67-
They may be sent by default on every client request, irrespective of the server `Accept-CH` response header, depending on the permission policy.
88+
The low entropy hints are those that don't give away much information that might be used to [fingerprint](/en-US/docs/Glossary/Fingerprinting) a user.
89+
They may be sent by default on every client request, irrespective of the server `Accept-CH` response header, depending on the [permission policy](https://wicg.github.io/client-hints-infrastructure/#policy-controlled-features).
6890
Low entropy hints are:
6991

7092
- {{HTTPHeader("Save-Data")}},
@@ -75,7 +97,7 @@ Low entropy hints are:
7597
## High entropy hints
7698

7799
The high entropy hints are those that have the potential to give away more information that can be used for user fingerprinting, and therefore are gated in such a way that the user agent can make a decision whether to provide them.
78-
The decision might be based on user preferences, a permission request, or the permission policy.
100+
The decision might be based on user preferences, a permission request, or a [permission policy](https://wicg.github.io/client-hints-infrastructure/#policy-controlled-features).
79101
All client hints that are not low entropy hints are considered high entropy hints.
80102

81103
## Critical client hints
@@ -108,6 +130,8 @@ Host: example.com
108130
Sec-CH-Prefers-Reduced-Motion: "reduce"
109131
```
110132

133+
In summary, `Accept-CH` requests all values you'd like for the page, while `Critical-CH` requests only the subset of values you must have on-load to properly load the page.
134+
111135
## Hint types
112136

113137
### User agent client hints
@@ -141,10 +165,35 @@ Headers include: {{HTTPHeader("Device-Memory")}}, {{HTTPHeader("Width")}}, {{HTT
141165
Network client hints allow a server to vary responses based on the user's choice, network bandwidth, and latency.
142166
Headers include: {{HTTPHeader("Save-Data")}}, {{HTTPHeader("Downlink")}}, {{HTTPHeader("ECT")}}, {{HTTPHeader("RTT")}}.
143167

168+
## Using client hints for responsive design
169+
170+
It's possible to use client hints for responsive design, for example to detect whether a mobile device or tablet is rendering your site.
171+
172+
An Android phone would send the following default client hints:
173+
174+
```http
175+
Sec-CH-UA: "Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"
176+
Sec-CH-UA-Platform: "Android"
177+
Sec-CH-UA-Mobile: ?1
178+
```
179+
180+
An Android tablet on the other hand would send the following:
181+
182+
```http
183+
Sec-CH-UA: "Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"
184+
Sec-CH-UA-Platform: "Android"
185+
Sec-CH-UA-Mobile: ?0
186+
```
187+
188+
The {{httpheader("Sec-CH-UA-Mobile")}} header can be used to determine whether the device is a mobile device or not. For hardware-specific use cases, the device model name and form factor can be requested via the high-entropy {{httpheader("Sec-CH-UA-Model")}} and {{httpheader("Sec-CH-UA-Form-Factors")}} hints.
189+
190+
For many responsive design needs, [media queries](/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries) may be more convenient and flexible. However, there may be cases where you don't have control over the individual stylesheets of a site, and need to vary the stylesheet served based on the device signature or some kind of user preference. There are client hints that mirror some of the "user preference" media queries, such as {{httpheader("Sec-CH-Prefers-Color-Scheme")}}, {{httpheader("Sec-CH-Prefers-Reduced-Motion")}}, and {{httpheader("Sec-CH-Prefers-Reduced-Transparency")}}.
191+
144192
## See also
145193

146194
- [Client Hints headers](/en-US/docs/Web/HTTP/Reference/Headers#client_hints)
147195
- [`Vary` HTTP Header](/en-US/docs/Web/HTTP/Reference/Headers/Vary)
148196
- [Client Hints Infrastructure](https://wicg.github.io/client-hints-infrastructure/)
149197
- [User Agent Client Hints API](/en-US/docs/Web/API/User-Agent_Client_Hints_API)
150-
- [Improving user privacy and developer experience with User-Agent Client Hints](https://developer.chrome.com/docs/privacy-security/user-agent-client-hints) (developer.chrome.com)
198+
- [Improving user privacy and developer experience with User-Agent Client Hints](https://developer.chrome.com/docs/privacy-security/user-agent-client-hints) on developer.chrome.com (2020)
199+
- [Migrate to User-Agent Client Hints](https://web.dev/articles/migrate-to-ua-ch) on web.dev (2021)

0 commit comments

Comments
 (0)