Fiverr Tech
Published in

Fiverr Tech

Detect mobile device
Photo by Plann on Unsplash

Detect mobile browsers without user agent string parsing: Client Hints

Should I use it already?

Browser distribution

Disadvantages of User-Agent Parsing

Implementation Examples

Browser Implementation with Fallback

const uaDataIsMobile = window.navigator.userAgentData?.mobileconst isMobile = typeof uaDataIsMobile === 'boolean'
? uaDataIsMobile
: legacyIsMobileCheck(window.navigator.userAgent)

Server Implementation with Fallback

const clientHintMobile = request.get('sec-ch-ua-mobile')const isMobile = clientHintMobile
? clientHintMobile.includes('1') // values: ?1 or ?0
: legacyIsMobileCheck(request.get('user-agent'))

In Conclusion

--

--

The technology behind the platform that’s changing the future of work

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store