Skip to content

Commit db11a0d

Browse files
ajitsinghkaleratscott
authored andcommitted
docs(service-worker): add staleWhileRevalidate strategy (angular#37301)
There is great workaround for implementing staleWhileRevalidate strategy in service-worker by setting strategy to freshness and timeout to 0u. Documented this in service worker config where all other strategies are documented Fixes angular#20402 PR Close angular#37301
1 parent 7793440 commit db11a0d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

aio/content/guide/service-worker-config.md

+15
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,21 @@ The Angular service worker can use either of two caching strategies for data res
193193

194194
* `freshness` optimizes for currency of data, preferentially fetching requested data from the network. Only if the network times out, according to `timeout`, does the request fall back to the cache. This is useful for resources that change frequently; for example, account balances.
195195

196+
197+
<div class="alert is-helpful">
198+
199+
You can also emulate a third strategy, [staleWhileRevalidate](https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#stale-while-revalidate), which returns cached data (if available), but also fetches fresh data from the network in the background for next time.
200+
To use this strategy set `strategy` to `freshness` and `timeout` to `0u` in `cacheConfig`.
201+
202+
This will essentially do the following:
203+
204+
1. Try to fetch from the network first.
205+
2. If the network request does not complete after 0ms (i.e. immediately), fall back to the cache (ignoring cache age).
206+
3. Once the network request completes, update the cache for future requests.
207+
4. If the resource does not exist in the cache, wait for the network request anyway.
208+
209+
</div>
210+
196211
### `cacheQueryOptions`
197212

198213
See [assetGroups](#assetgroups) for details.

0 commit comments

Comments
 (0)