Network Information

The Network Information component provides access to the Network Information API, allowing your Progressive Web App to detect and respond to changes in the user's network connection. This enables you to create adaptive experiences that optimize content delivery based on connection quality and type.

This component is particularly useful for:

  • Adaptive content loading based on connection speed

  • Video/image quality adjustment for bandwidth optimization

  • Automatic quality degradation on slow connections

  • Data saver mode detection and optimization

  • Preloading strategies based on connection type

  • Network-aware caching policies

  • Offline/online state detection

  • User experience optimization for different connection types

  • Bandwidth-conscious feature toggling

Browser Support

The Network Information API is currently supported in Chromium-based browsers (Chrome, Edge, Opera) and partially in Firefox. Safari does not support this API.

Supported Properties:

  • effectiveType: Widely supported (Chrome, Edge, Opera, Firefox)

  • downlink: Widely supported (Chrome, Edge, Opera)

  • rtt: Widely supported (Chrome, Edge, Opera)

  • saveData: Widely supported (Chrome, Edge, Opera, Firefox)

  • type: Limited support (Chrome, Edge on some devices)

circle-exclamation

Usage

Basic Network Detection

Adaptive Image Loading

Video Quality Adjustment

Conditional Preloading

Data Saver Mode Detection

Network-Aware Feature Toggling

Connection Type Monitoring

Parameters

None

Actions

None - This component automatically monitors network changes and emits events.

Targets

None

Events

pwa--network-information:change

Dispatched when the component is initialized and whenever the browser detects a change in network conditions (e.g., switching from Wi-Fi to cellular, connection quality changes).

Payload: {connection}

The connection object contains:

  • effectiveType (string): The estimated effective connection type

    • Values: 'slow-2g', '2g', '3g', '4g'

    • Represents the effective network quality based on observed RTT and downlink values

  • downlink (number): Estimated downlink speed in megabits per second (Mbps)

    • Example: 10.5 means 10.5 Mbps

  • rtt (number): Estimated round-trip time in milliseconds

    • Lower values indicate better latency

    • Example: 50 means 50ms latency

  • saveData (boolean): Whether the user has enabled data saver mode in their browser

    • true: User wants to minimize data usage

    • false: Normal data usage

  • type (string, optional): The connection medium (when available)

    • Values: 'bluetooth', 'cellular', 'ethernet', 'wifi', 'wimax', 'none', 'other', 'unknown'

    • Not widely supported on all devices/browsers

Example:

pwa--network-information:unsupported

Dispatched when the Network Information API is not supported by the browser.

No payload

Example:

Best Practices

  1. Always provide fallbacks: Not all browsers support this API - design for graceful degradation

  2. Respect Data Saver mode: When saveData is true, minimize data usage

  3. Don't rely on exact values: Connection metrics are estimates, not precise measurements

  4. Combine with actual performance: Use alongside Network Timing API for real measurements

  5. Test on real devices: Emulated network conditions may not reflect real-world behavior

  6. Update gradually: Don't make sudden quality changes that disrupt user experience

  7. Cache aggressively: Use the information to inform caching strategies

  8. Monitor changes: Connection can change frequently - handle multiple events

  9. Inform users: Let users know when you're adapting content based on their connection

  10. Allow overrides: Provide manual quality controls for user preference

Connection Type Reference

Effective Types

Type
Minimum RTT
Maximum Downlink
Use Case

slow-2g

2000ms

0.05 Mbps

Text-only, extreme optimization

2g

1400ms

0.07 Mbps

Lightweight content, minimal images

3g

270ms

0.7 Mbps

Standard content, compressed media

4g

0ms

Full quality, preloading enabled

Adapting Content by Connection

Troubleshooting

API not supported

Browser: Safari, older browsers

Solution: Always listen for the unsupported event and provide fallback behavior:

Inaccurate estimates

Issue: Connection metrics don't match actual performance

Solutions:

  • Use metrics as estimates, not absolute values

  • Combine with actual performance measurements (Resource Timing API)

  • Track real download times and adjust accordingly

  • Test on real devices and connections

Connection type unavailable

Issue: connection.type returns undefined

Cause: Many devices don't expose the connection medium

Solution: Rely on effectiveType, downlink, and rtt instead

Frequent quality changes

Issue: Content quality switches too often, disrupting user experience

Solutions:

  • Implement debouncing to avoid rapid changes

  • Set thresholds before switching quality

  • Only change quality at natural breakpoints (page load, user action)

Data Saver not detected

Issue: saveData always returns false

Cause: User hasn't enabled Data Saver in browser settings

Solution: This is expected behavior - only optimize when explicitly requested by the user

Last updated

Was this helpful?