Best practices for handling server offline (i.e. Network Error) #1476
-
I have implemented the OfflineExchange and have a query using requestPolicy: "cache-and-network". If I start my app with the server on-line to first populate the cache and then stop the server I'm seeing the query response come from the cache (as it should) and the network request failing without bubbling up the "Network Error". So, in this scenario using the OfflineExchange is working great. But, what if I still want to know that the query response came from the cache and is not fresh so that I can notify the user that the data they are viewing is stale? What would be the best way to do that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I'd combine some logic to detect offline states, an example of which you can find in the source itself: https://github.com/FormidableLabs/urql/blob/65e15c568c9a0755894dfb02995f0262e1fe75f9/exchanges/graphcache/src/offlineExchange.ts#L59 This should allow you to detect temporary offline states and notify the user of them. Alternatively, a lot of web apps also just use the window online/offline events. While these events aren't as accurate (and will in your case not fire) they often can be used to switch an app into the "offline" state without requiring a request first. The disadvantage of only relying on these though is that they may not fire if a network device still has an apparently working connection that just isn't transferring (think of a broken 3G connection that has signal but where the network is overloaded) |
Beta Was this translation helpful? Give feedback.
I'd combine some logic to detect offline states, an example of which you can find in the source itself: https://github.com/FormidableLabs/urql/blob/65e15c568c9a0755894dfb02995f0262e1fe75f9/exchanges/graphcache/src/offlineExchange.ts#L59
With the
errorExchange
which can notify you of any error.This should allow you to detect temporary offline states and notify the user of them. Alternatively, a lot of web apps also just use the window online/offline events.
While these events aren't as accurate (and will in your case not fire) they often can be used to switch an app into the "offline" state without requiring a request first. The disadvantage of only relying on these though is that they ma…