Home Web Front-end CSS Tutorial Freezing User-Agent Strings

Freezing User-Agent Strings

Apr 11, 2025 am 11:20 AM

Chrome and other major browsers are about to freeze the User-Agent string, which has attracted widespread attention. This means that the User-Agent string still exists (appears in the request header and is accessible in JavaScript via navigator.userAgent ), but its functionality for detecting browsers/platforms/versions will decrease over time. Google officially explained that this move is to protect user privacy and prevent fingerprint recognition.

The front-end development field has always been recommended: Avoid User-Agent detection . Many websites use User-Agent detection incorrectly, resulting in more harm than good. The best way to avoid User-Agent detection is to test based on actual requirements .

Need to test whether the browser supports specific features? Test the feature directly, rather than relying on abstract concepts that assume that the browser should support the feature.

In JavaScript, testing many features is very simple, just check if their API exists:

 if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(showPosition);
} else {
  console.warn("Geolocation not supported");
}
Copy after login

In CSS, you can use the @supports native mechanism:

 @supports (display: grid) {
  .main {
    display: grid;
  }
}
Copy after login

This can be detected by the JavaScript API that returns a Boolean value:

 CSS.supports("display: flex");
Copy after login

Not all web platform features are so easy to test, but usually do not require User-Agent probing. If you encounter difficulties, you can see if Modernizr provides the corresponding test, which is the gold standard for functional testing because it is well tested and handles edge cases that you may not foresee. Use the Modernizr library to get clear logical branches:

 if (Modernizr.requestanimationframe) {
  // Support } else {
  // Not supported}
Copy after login

If you really need to know the browser type, platform, and version, you can use User-Agent Client Hints (UA-CH).

For example, to obtain platform information, you can set the Sec-CH-Platform header in the request, which theoretically can be obtained in the response. This requires proactive requests for information, thereby avoiding problematic privacy fingerprint recognition problems. Similarly, there are Sec-CH-Mobile headers for detecting mobile devices, but this raises some questions: Who decides what a "mobile" device is? How should we make decisions based on this information?

It is usually desirable to understand browser, platform, and version information on the server side (sending different code in different situations), which is as important as the client side, but cannot be tested on the server side. The frozen User-Agent string should be able to last long enough for the server to be able to migrate to using UA-CH.

Jon Arne Sæterås expressed concerns:

I've been working in the mobile web field for over 15 years and I know that many websites, big and small, rely on device detection based on User-Agent headers. From a Google perspective, switching to UA-CH seems easy, but that's exactly where the teams driving this change don't understand its impact:

Device detection-based functions are crucial and widely used, not only in front-end code. Large software systems and their backend code rely on device detection, as does the entire infrastructure stack.

In my main code base, we do a small amount of server-side UA detection. We use a Rails gem called Browser, which exposes UA-derived information in a friendly API. I can write like this:

 if browser.safari?
  # Safari code end
Copy after login

We also expose the information in this gem on the client for use on the client. There are only a few use instances for the front-end and back-end, and these instances seem to be easily handled in other ways.

In the past, it was a bit tricky to pass front-end information back to the server in a way that was useful when the first page was loaded (because UA didn't know the viewport size and other information). I remember doing some very clever operations, loading a skeleton page, executing a small piece of JavaScript code to measure viewport width and screen size, then setting a cookie and forcibly refreshing the page. If a cookie exists, the server will have the required information and the skeleton page will not be loaded at all in these requests.

This is complicated, but the server can get the viewport width information on the server side, which is very useful for operations such as sending small screen resources (such as different HTML), which is otherwise impossible.

I mentioned this because UA-CH should not be confused with normal client prompts. We should be able to configure the server to send Accept-CH header and then let the client code whitelist the information to be sent back, for example:

<meta content="DPR, Viewport-Width" http-equiv="Accept-CH">
Copy after login

This means that the server can get this information from the client in subsequent page loading. This is a nice API, but Firefox and Safari don't support it. If both browsers are interested in UA-CH because of frozen UA strings, I wonder if it will be improved.

Freezing User-Agent Strings

The above is the detailed content of Freezing User-Agent Strings. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles