4 ms·
Relying on the UA string for browser detection is an anti-pattern. Instead, you should do feature detection. Modernizr [1] is nice, but you can also just do it
by spiralganglion 6y ago
Relying on the UA string for browser detection is an anti-pattern. Instead, you should do feature detection. Modernizr [1] is nice, but you can also just do it yourself. Look into CSS @support too.
[1] https://modernizr.com https://modernizr.com
- bichiliad 6y agoIt looks like using an approach like Modernizr relies on downloading the detection code, running it, and then potentially kicking off additional downloads for polyfills. Or it would mean eagerly downloading polyfills on browsers that don't need them. In the former case, you pay the cost of a second network request, especially in older browsers that don't support HTTP/2, and in the latter case, you send a potentially large number of bytes of polyfill code to clients that don't use them. What makes using a browser's agent string an anti-pattern?
- omnimus 6y agoThe fact that you have no guarantee the agent string is correct. No guarantee your polyfills and your code works.
- bichiliad 6y agoYou don't really have much of a guarantee about anything from the client when they make a request. You have to take them at good faith when they report a user-agent to you. Plus, from real-world experimentation, I'm relatively confident that the performance benefits of the solution I mentioned outweigh the cost of supporting browsers that spoof their user agents: https://news.ycombinator.com/item?id=23662405 https://news.ycombinator.com/item?id=23662405