navigator.connection Gates Heavy Media
navigator.connection exposes effectiveType, saveData and rtt on Chromium; feature-detect it, honor saveData first, then pick assets by measured network quality.
Code
// Network Information API: Chromium exposes effectiveType, saveData, rtt
// and downlink; feature-detect because other engines omit it.
const connection = navigator.connection;
if (!connection) {
console.log('NetworkInformation unavailable — ship the full assets');
} else {
// saveData means the user asked for Data Saver: respect it before
// anything else, regardless of measured quality.
if (connection.saveData) {
console.log('saveData on — serve text-only variant');
} else {
const light = connection.effectiveType === '2g' || connection.effectiveType === 'slow-2g';
console.log(`effectiveType ${connection.effectiveType}, rtt ${connection.rtt}ms -> ${light ? 'poster + low bitrate' : 'full media'}`);
}
}Output
Captured from a sandboxed run of this snippet in the build pipeline.
NetworkInformation unavailable — ship the full assetsWalking through the code — navigator.connection Gates Heavy Media
Top-level bindings connection hold the demo state — nothing sits inside a function scope.
Caveats
- Everything it calls is documented as part of the language core.
- The snippet leans on
navigator— browser-only surfaces a bare Node process cannot provide.
Using it
The run emits 1 labeled section — saveData on — serve text-only variant — one per step.
Key statements
const light = connection.effectiveType === '2g' || connection.effectiveType === 'slow-2g';
const connection = navigator.connection;
Interactive Tools for This Topic
CIDR to IP ListExpand an IPv4 CIDR block into its individual addresses, showing network, broadcast, total count, and a bounded list you can copy.DSN Connection String ParserParse PostgreSQL, MySQL, Redis, MongoDB, and AMQP DSN URLs locally with masked credentials, multi-host support, default ports, and SSL/pool inspection.IPv6 CompressorCompress an IPv6 address per RFC 5952 and show its fully expanded form, validating every hextet.MAC Address FormatterNormalize a MAC address from any separator style into colon, hyphen, Cisco dotted, and plain forms, with 12-hex-digit validation and case control.
FAQ
- Is a package install part of the setup?
- No. Installation is unnecessary because only built-in APIs are used. Its state lives in module-level bindings: `connection`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.