I am trying to implement different cache strategies using ServiceWorker. For the following strategies the way to implement is completely clear:
- Cache first
- Cache only
- Network first
- Network only
For example, while trying to implement the cache-first strategy, in the fetch hook of the service-worker I will first ask the CacheStorage (or any other) for the requested URL and then if exists respondWith
it and if not respondWith
the result of network request.
But for the stale-while-revalidate strategy according to this definition of the workbox, I have the following questions:
- First about the mechanism itself. Does stale-while-revalidate mean that use cache until the network responses and then use the network data or just use the network response to renew your cache data for the next time?
- Now if the network is cached for the next time, then what scenarios contain a real use-case of that?
- And if the network response should be replaced immediately in the app, so how could it be done in a service worker? Because the hook will be resolved with the cached data and then network data could not be resolved (with
respondWith
).