A polyfill for the W3C Network Information API that provides real-time network connection information when the native API is not available.
- π Automatic installation - Just import and use with WICG classification
- π Real-time measurements - Active network speed testing using Cloudflare's infrastructure
- β‘ Lightweight - Less than 3kB minified and compressed
- π― Multiple classification standards - WICG, Firefox DevTools, Chrome DevTools, or custom
- π³ Tree-shakeable - Import only what you need
- π§ Fully configurable - Customize thresholds and measurement parameters
Live playground β
Or test in browser console:
import('https://esm.sh/jsr/@esroyo/network-information-api-polyfill').then(() => {
navigator.connection?.addEventListener('change', (e) => {
console.log('π Network changed:', e.detail);
});
});
Automatic (recommended for most users):
import '@esroyo/network-information-api-polyfill';
// Now available on navigator
navigator.connection?.addEventListener('change', (event) => {
console.log('Network:', event.detail.effectiveType, event.detail.downlink + 'Mbps');
});
Manual with tree-shaking:
import { installNetworkInformationPolyfill } from '@esroyo/network-information-api-polyfill/pure';
import CLASSIFICATION_FIREFOX from '@esroyo/network-information-api-polyfill/classifications/firefox';
installNetworkInformationPolyfill({
classificationTable: CLASSIFICATION_FIREFOX,
measurementCount: 3,
periodicMeasurement: true
});
Standalone instance:
import { createNetworkInformation } from '@esroyo/network-information-api-polyfill/network-information';
import CLASSIFICATION_CHROME from '@esroyo/network-information-api-polyfill/classifications/chrome';
const networkApi = createNetworkInformation({
classificationTable: CLASSIFICATION_CHROME,
periodicMeasurement: true
});
networkApi.addEventListener('change', (event) => {
console.log('Network changed:', event.detail);
});
This polyfill may not reflect throttled speeds when using browser DevTools network throttling.
DevTools throttling is applied synthetically, but this polyfill uses the Performance Resource Timing API which reports actual network timings, not artificial throttling.
For accurate testing: Use real mobile networks, network-level throttling, or deploy to staging environments.
downlink
: Downlink speed in Mbpsuplink
: Uplink speed in Mbps (estimated)rtt
: Round-trip time in millisecondseffectiveType
: Connection classification ('slow-2g'
,'2g'
,'3g'
,'4g'
)saveData
: Whether data saving mode is enabledtype
: Connection type (always'unknown'
in polyfill)
measure()
: Manually trigger a network measurementgetConnectionInfo()
: Get current connection informationdispose()
: Clean up resources and stop periodic measurementsaddEventListener()
/removeEventListener()
: Event handling
interface NetworkInformationConfig {
classificationTable: ConnectionClassification[]; // Required
origin?: string; // Default: 'https://speed.cloudflare.com'
measurementCount?: number; // Default: 2
baseMeasurementSize?: number; // Default: 100000 bytes
measurementSizeMultiplier?: number; // Default: 2
periodicMeasurement?: boolean; // Default: false
measurementInterval?: number; // Default: 30000ms
estimatedServerTime?: number; // Default: 10ms
estimatedHeaderFraction?: number; // Default: 0.005
}
Based on the official specification:
Type | Max Downlink | Min RTT | Description |
---|---|---|---|
slow-2g |
< 50 kbps | > 1400ms | Very slow connection |
2g |
< 70 kbps | > 270ms | Slow connection |
3g |
< 700 kbps | - | Moderate connection |
4g |
- | - | Fast connection |
import CLASSIFICATION_FIREFOX from '@esroyo/network-information-api-polyfill/classifications/firefox';
import CLASSIFICATION_CHROME from '@esroyo/network-information-api-polyfill/classifications/chrome';
Different thresholds based on browser DevTools standards. See the classification files for detailed specifications.
const customClassification = [
{ type: 'slow-2g', maxDownlink: 0.1, minRtt: 1000 },
{ type: '2g', maxDownlink: 0.5, minRtt: 500 },
{ type: '3g', maxDownlink: 2.0, minRtt: 200 },
{ type: '4g' }
];
- Use
measurementCount: 1
for minimal bandwidth usage - Call
dispose()
to prevent memory leaks - Balance accuracy vs. resource usage with appropriate intervals
Modern browsers with fetch API support.
Contributions welcome! Especially new classification standards, bundle optimizations, and performance improvements.
MIT License - see LICENSE file for details.