Is there a way to detect that a response has come from cache when using the cache-and-network fetch policy

Hi All,

So I have an app that I would like to display some content from the server. If this server ever goes down, I still want to display the content from cache, but ideally I’d always be showing thee latest data, so i’ve used a fetch policy of cache-and-network.

The problem is, since the information is quite time sensitive, I’d like to be able to display a message to the user when the request failed and the data had to come from the cache. I was wondering if there’s a way to detect that the response came from the cache in Apollo? If not, does anyone know of another way of achieving something similar?

Thanks a lot for any help you’re able to give.

Max

@M4sterShake one way you can handle this is to leverage the onError Link to intercept errors that happen when attempting to run operations against your server, then decide how you want to handle that error in your application. The Advanced error handling with Apollo Link section of the docs shows how to use onError. When a network issue occurs, you could flag that in your local state somewhere (e.g. store it in a reactive variable), then leverage this in your component when you want to show a “network request failed” message. If you decide to use a reactive variable, the Storing local state in reactive variables section of the docs should help show how you can leverage that reactive variable in the query you’re watching in your component, so that when you flag the error in your link, it will automatically be picked up in your component.

1 Like

Thanks that helps a lot! :slight_smile:

1 Like