2.1.0 ā€¢ Published 7 days ago

client-hints v2.1.0

Weekly downloads
-
License
Unlicense
Repository
github
Last release
7 days ago

client-hints npm.io

šŸ•µļøā€ā™‚ļø Parse client hints headers

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
contentDpr, contentDevicePixelRationumberContent-DPRImage device pixel ratioDeprecated
deviceMemorynumberDevice-MemoryAppr. available RAMExperimental
downlinknumberDownlinkNetwork speed (Mbps)Experimental
dpr, devicePixelRationumberDPRDevice pixel ratioDeprecated
dpr, devicePixelRatiostringContent-DPRImage device to pixel ratioExperimental
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
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 Intruction 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.1.0

7 days ago

2.0.0

7 days ago

1.1.0

7 months ago

1.0.0

7 months ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.0

2 years ago