2.2.2 • Published 5 months ago

client-hints v2.2.2

Weekly downloads
-
License
Unlicense
Repository
github
Last release
5 months ago

client-hints npm.io

šŸ•µļøā€ā™‚ļø Parse client hints headers and provide a convenient API to access device information and user preferences

Use client hints, where available

import { ClientHints } from "client-hints";
import { userAgentParser } from "some-user-agent-parser-library";

app.get("/api-endpoint", (req, res) => {
  const hints = new ClientHints(req);

  const isMobile =
    hints.mobile ?? // cheap
    userAgentParser(req.get("user-agent")).device?.type === "mobile"; // more expensive

  const browserName =
    hints.vendorName ?? // cheap
    userAgentParser(req.get("user-agent")).browser?.name; // more expensive

  console.log(JSON.stringify(hints, null, 2)); // Serialises all available hints

  res.send(`${isMobile ? "Mobile" : "Desktop"} browser: ${browserName}`);
});

Available detections

All detections return undefined if the relevant header is not available.

FeatureTypeHeaderMeaningAdoption level
arch, architecturestringSec-CH-UA-ArchCPU architectureExperimental
bitnessnumberSec-CH-UA-BitnessCPU bitness (32, 64)Experimental
contentDpr, contentDevicePixelRationumberContent-DPRImage device pixel ratioDeprecated
deviceMemorynumberDevice-MemoryAppr. available RAMExperimental
downlinknumberDownlinkNetwork speed (Mbps)Experimental
dpr, devicePixelRationumberDPRDevice pixel ratioDeprecated
ect, effectiveConnectionTypestringECTNetwork profile (G)Experimental
fetchDest, fetchDestinationstringSec-Fetch-DestResource type
fetchModestringSec-Fetch-ModeNavigation type
fetchSitestringSec-Fetch-SiteRelationship to origin
fetchUserbooleanSec-Fetch-UserWas the request triggered by user action
gpcbooleanSec-GPCConsents to sell or share personal informationExperimental
mobilebooleanSec-CH-UA-MobileBoolean: Is this a mobile deviceExperimental
modelstringSec-CH-UA-ModelDevice modelExperimental
platformstringSec-CH-UA-PlatformOperating system nameExperimental
platformVersionstringSec-CH-UA-Platform-VersionOperating system versionExperimental
prefersColorSchemestringSec-CH-Prefers-Color-SchemeUser's preferred color schemeExperimental
prefersReducedMotionbooleanSec-CH-Prefers-Reduced-MotionUser prefers reduced motionExperimental
prefersReducedTransparencybooleanSec-CH-Prefers-Reduced-TransparencyUser prefers reduced transparencyExperimental
purposestringSec-PurposeResource purpose (prefetch)Experimental
vendorNamestringSec-CH-UA / Sec-CH-UA-Full-Version-ListBrowser NameExperimental
vendorVersionstringSec-CH-UA-Full-Version / Sec-CH-UA / Sec-CH-UA-Full-Version-ListBrowser versionExperimental
viewportWidthnumberViewport-WidthLayout viewport widthDeprecated
widthnumberWidthResource desired widthDeprecated

Client Hints Instructions Response Header

Set the value of Accept-CH header to include the headers you want the browse to send.

Accept-CH: Sec-CH-UA-Mobile,Sec-CH-UA-Full-Version,Sec-CH-UA-Full-Version-List,Sec-CH-UA-Model,Sec-CH-UA-Platform,Sec-CH-UA-Platform-Version,ECT

Include all available hints:

Accept-CH: *
2.2.2

5 months ago

2.2.1

1 year ago

2.2.0

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

1.1.0

2 years ago

1.0.0

2 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.0

3 years ago