Course contentsShow
← Web Development
Lesson 2324 of 2,98242. Progressive Web AppsFree lesson

Network Information API

Explore the Network Information API to detect connection type, speed, and adapt UI based on network quality.

Network Information API

What you'll learn: How to detect a user's network quality and speed to deliver adaptive experiences in your Progressive Web App.

What Is the Network Information API?

The Network Information API exposes information about the user's connection type and quality. Think of it as a speedometer for the internet — it tells you whether your user is on blazing-fast fiber, sluggish 2G, or somewhere in between. With this data, you can adapt your app's behavior: serve lower-resolution images on slow connections, defer non-critical downloads, or warn users before loading heavy content.

Core Concepts

The API is accessed through navigator.connection (or navigator.mozConnection / navigator.webkitConnection in some browsers). Key properties include:

  • effectiveType: A string like 'slow-2g', '2g', '3g', or '4g'
  • downlink: Estimated download speed in Mbps
  • rtt: Round-trip time in milliseconds (latency)
  • saveData: Boolean indicating if the user has enabled data-saving mode

The change event fires when network conditions change, letting you react dynamically.

Practical Use

if ('connection' in navigator) {
  const connection = navigator.connection;
  
  console.log(`Connection type: ${connection.effectiveType}`);
  console.log(`Downlink speed: ${connection.downlink} Mbps`);
  
  if (connection.effectiveType === 'slow-2g' || connection.effectiveType === '2g') {
    // Load low-res images, disable autoplay
  }
  
  connection.addEventListener('change', () => {
    console.log('Network conditions changed');
  });
}

Always check for API support and provide sensible defaults for browsers that don't implement it yet.

Key Takeaway: The Network Information API lets you detect connection quality and adapt your PWA's UI and resource loading to provide better experiences across varying network conditions.