I was running into this in Google Chrome and found my way to the Online/Offline Event Detection page for Electron (which is based on Google Chrome). It turns out that virtualization software and network adapters can prevent your browser from detecting when it has gone offline.
In my case, I was turning Wifi on and off but navigator.onLine
was always returning true
. The problem was that I was connected to a VPN and, even after I turned Wifi off, the VPN still registered as "connected" (I imagine it might have eventually figured out that it was disconnected, but it never registered this in my testing). If I manually disconnected from the VPN, then turning Wifi on and off started immediately being reflected in Google Chrome and navigator.onLine
would correctly return false
.
So for folks running into this issue, the answer is that you probably have some other software on your computer that is preventing your browser from detecting the correct network status. This could be a VPN, it could be virtualization software, etc.
For folks developing web apps, this poses an annoying challenge. I've noticed that Firebase's Firestore service (a proprietary product) will just assume it is offline and go into "offline mode" if it is unable to connect to the server after 10 seconds. I imagine it does this because there isn't any surefire way of detecting online/offline status from just javascript.
Additional details from MDN
In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking.