From c8fef554f70c7f86835ea3fb3f960ca4acd01f8a Mon Sep 17 00:00:00 2001 From: Ajit Singh Date: Sat, 6 Jun 2020 08:44:12 +0530 Subject: [PATCH] docs(service-worker): add staleWhileRevalidate strategy 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 #20402 --- aio/content/guide/service-worker-config.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/aio/content/guide/service-worker-config.md b/aio/content/guide/service-worker-config.md index 7a90a1d988f3b..58bce456d41a9 100644 --- a/aio/content/guide/service-worker-config.md +++ b/aio/content/guide/service-worker-config.md @@ -193,6 +193,21 @@ The Angular service worker can use either of two caching strategies for data res * `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. + +
+ +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. +To use this strategy set `strategy` to `freshness` and `timeout` to `0u` in `cacheConfig`. + +This will essentially do the following: + +1. Try to fetch from the network first. +2. If the network request does not complete after 0ms (i.e. immediately), fall back to the cache (ignoring cache age). +3. Once the network request completes, update the cache for future requests. +4. If the resource does not exist in the cache, wait for the network request anyway. + +
+ ### `cacheQueryOptions` See [assetGroups](#assetgroups) for details.