Is there a way to detect when URQL determines it is offline? #1167
-
I'm working on a part of my code dealing with offline detection. Since URQL (rightly) uses the ability to reach the server as it's detector for going offline, I wondered if there was a way to detect/listen to when URQL thinks it is offline? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
There are multiple indicators, but basically we're currently working with the assumption that knowing when It then uses what you pass for |
Beta Was this translation helpful? Give feedback.
-
Yea, I think the major set of use cases for an application knowing it is "offline" is to be able to display the information for the user and to keep track of transitional stages - e.g. offline -> online vs logged in online to logged in offline. The difficultly lies in where I said "rightly". By using server connectivity as its gauge, URQL allows for extensive offline mode development simply by stopping the server (works great with a local dev Apollo Server). Also supports a use case we have for a training version which will be installed on Surface Pros for use disconnected from the internet where using But, here lies the rub, we have to reliably know when URQL transitions to offline mode. Hence my question. |
Beta Was this translation helpful? Give feedback.
-
Sound advice. For the most part, the thing we're writing will either be offline (because they are in a restricted area where they are not allowed to connect) or will be online (which is when the work they queued up in the restricted area will be sent to the server). Because of using a piggyback authentication method, I have a (fairly) reliable way of detecting if the system is producing a Network Error before I attempt to execute the URQL mutations. I'll probably attempt to trap it there for the time being. I do like your idea of tapping the exchanges. I've thought about trying to write a custom exchange to tap into the flow for some other purposes (including a long term replacement for my current piggyback system) but I'm not comfortable enough yet in understanding the Wonka interface to attempt it. I'm curious, is there a specific reason for using Wonka vs RxJs? Not a criticism because I fully understand how designing a system can have context specific advantages, however, it seems like RxJs might (and I use that term with care) have done the job while bringing a significant ecosystem along with it. |
Beta Was this translation helpful? Give feedback.
There are multiple indicators, but basically we're currently working with the assumption that knowing when
urql
thinks it's online again is more important. The main indicator that@urql/exchange-graphcache
contains to check whether it's offline is passive. It looks at results and if it sees that a network error of a particular shape and message has occurred it assumes it's been offline: https://github.com/FormidableLabs/urql/blob/f3cf0c9685f41257b86b6b3a88512ccfc7883b02/exchanges/graphcache/src/offlineExchange.ts#L58-L65It then uses what you pass for
storage.onOnline
to determine when to assume it's back online. So you can in theory piggy back on that and makestorage.onOnline
more compl…