What are some service worker’s caching strategies do you know?

Technology CommunityCategory: PWAWhat are some service worker’s caching strategies do you know?
VietMX Staff asked 5 months ago

Those three caching strategies are framework agnostic:

  • Cache only – The SW expects to find the requested assets already here.
  • Network only – This strategy is exactly the opposite of the previous one: we always access the network, without even querying the cache.
  • Stale while revalidate – Similarly to the cache only strategy, the goal is to ensure a fast responses by delivering the data from the cache. However, while the client request is served, a separate request is triggered to the server to fetch a newer version, if available, and store it into the cache.

Angular provides only two kind of caching strategy:

  • Performance (default) – Here the goal is to optimise the response time. If a resource is available in the cache, this version is delivered. Otherwise a network request is executed to fetch and then cache it.
  • Freshness – Used when it is required to deliver the latest data from the network. We can specify a timeout after which the request falls back to the cache and tries to deliver the required data from there.