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.

JavascriptData Fetching

Code

javascriptsnippet
16 lines
// 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 assets

Walking 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

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`.