Active Connectivity Checks
What you'll learn: How to verify real internet connectivity by making actual network requests, not just trusting browser detection.
Why Active Checks Matter
You've learned that navigator.onLine can lie—it only tells you if the device thinks it has a network connection, not if the internet is actually reachable. A device might be connected to a router with no internet, or behind a captive portal requiring login. That's where active checks come in.
Making Real Requests
Active connectivity checks work by sending lightweight HTTP requests to a known, reliable endpoint and checking if they succeed:
async function checkRealConnectivity() {
try {
// HEAD request = faster, no body downloaded
const response = await fetch('https://www.google.com/favicon.ico', {
method: 'HEAD',
mode: 'no-cors', // Avoid CORS issues
cache: 'no-store' // Don't use cached response
});
return true; // If we got here, internet works
} catch (error) {
return false; // Network request failed
}
}
You can also use a GET request to your own server's health endpoint if you want more control.
Think of It Like This
Browser detection is like checking if your phone shows WiFi bars. Active checking is like actually trying to load a webpage to see if it works. The bars might lie; loading the page tells the truth.
Timing Matters
Don't spam these checks—they cost bandwidth and battery. Typical patterns:
- Check on
onlineevent to confirm - Check before critical operations
- Periodic checks every 30-60 seconds when connectivity is uncertain
Key Takeaway: Active connectivity checks use real HEAD or GET requests to verify actual internet access, overcoming the limitations of browser-only detection methods like navigator.onLine.