GoogleChromeLabs / react-adaptive-hooks

Deliver experiences best suited to a user's device and network constraints
Apache License 2.0
5.11k stars 114 forks source link

Offline Detection? #35

Open rrLucho opened 4 years ago

rrLucho commented 4 years ago

is there a way to detect offline state in the network, so we can serve different content, using the hooks?

mlampedx commented 4 years ago

I think this would be a great addition to the useNetworkStatus hook. I think the online/offline events API could be used.

JCofman commented 4 years ago

Hi :)

I created a pull request for this feature -> #45

anton-karlovskiy commented 4 years ago

We shouldn't land support for navigator.onLine API because the API itself is unreliable.

https://natalian.org/2012/07/16/navigator.onLine/ https://blog.superhuman.com/building-reliable-apps-on-unreliable-networks-3f75743fc457

The way browsers implement navigator.onLine means that you would likely need to do something much more custom to get what you are actually after.

Per 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.

JCofman commented 4 years ago

As mentioned by @anton-karlovskiy the method used for the pull request is fragile but if you still want to use offline detection using the navigator.onLine API you can checkout -> https://github.com/jaredpalmer/the-platform#usenetworkstatus

I think we can close this issue? :)