Apollo Fetch Policies
Apollo Client provides fetch policies that determine how a query interacts with the cache and network.
Common policies include:
cache-first
Use cached data when available. Otherwise request from the network.
network-only
Always request fresh data from the network and update the cache with the result.
cache-only
Read from the cache without making a network request.
no-cache
Request from the network without storing the result in the cache in the normal way.
cache-and-network
Return cached data when available while also requesting fresh network data.
The correct policy depends on the data.
For relatively stable reference data, cache-first can reduce unnecessary requests.
For rapidly changing data such as a live operational dashboard, network-only or cache-and-network may be more appropriate.
Do not choose network-only everywhere simply because freshness sounds safer. Doing so can throw away many of the benefits of client-side caching.
Conversely, do not use cache-first for data where stale information can create serious business problems without considering how and when the cache is invalidated.
Fetch Policies
Cache first, network if missing
Good for data where cached results are useful and acceptable.
Always fetch from network
Useful when a fresh server result is required.
Cache immediately and refresh from network
Useful when fast cached UI and eventual freshness are both desirable.
Cache only
Useful when the application explicitly wants to avoid a network request.
Network without normal cache storage
Useful when the result should not participate in normal cache storage.
Fetch policies control cache and network behavior.
No single fetch policy is correct for every query.
Freshness requirements should drive policy decisions.
Cache behavior and invalidation are part of application design.
Which fetch policy requests network data while also allowing cached data to be displayed first when available?