Network Error fallback on cache
See original GitHub issueHey guys 👋 ,
There was a previous issue on this topic #1653 but there wasn’t a resolution and it was autoclosed.
What I am wondering is how you can fallback on the cache when an apollo query fails due to a network error. Specifically I am working in the mobile realm where connections can be much more flakey and I am finding that if a user goes offline temporarily, I want to fallback on cached data. As far as I can tell the only option I have is to check data.error
for a networkError
and if so query my own cache and return that data to the children of my query component.
While this is possible, I am wondering if this is the best solution?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:14 (4 by maintainers)
Top Results From Across the Web
Managing fallback responses - Chrome Developers
Implementing a fallback is an alternative to caching behaviors that strategies like network-first or stale-while-revalidate provide.
Read more >What is the meaning of NETWORK and FALLBACK fields in ...
FALLBACK: An optional section specifying fallback pages if a resource is inaccessible. The first URI is the resource, the second is the fallback ......
Read more >Create an offline fallback page - web.dev
Learn how to create a simple offline experience for your app.
Read more >Retrying Operations | urql Documentation
In case of a network error, e.g., when part the infrastructure is down, but a fallback GraphQL endpoint is available, e.g., from a...
Read more >Avoiding fallback in distributed systems - Amazon AWS
It was hard to test the distributed fallback case; even if we had simulated the cache failure, we wouldn't have found the problem,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @mbaranovski, that is what I am currently doing, but it doesn’t work as expected. This is due to an issue documented in react-apollo, when the server is unreachable or if the user is offline, apollo throws an unhandled exception that is not part of
networkError
, it is just a message indata.error
that says something like “Network error: Network request failed.” . This error needs to be caught on every query/mutation component written, you can write a HOC query/mutation that does it, but it is still annoying, my code ended up looking like this:Because of how
cache-and-network
policy works, if the network request fails, I can’t just escape hatch out (like throw the error and have it be caught by apollo and fallback on the cache), I have to manually catch the error myself, and manually query the store, and catch any errors from reading from the store and return some other UI if that fails.Essentially, cache request will succeed, if network request then fails in the
cache-and-network
policy, it crashes from the unhandled exception, if I return something different in the error event it overwrites the render from thecache
response, so I am reading the cache on error and returning it if available.@mbaranovski
How about tell us how we should find where in our code this error is thrown? Line number? file name? Current cryptic error message is absolutely uninformative.