Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache-first Service Worker: how to bypass cache on updated assets?

Here is the scenario:

You have a site that currently cached via a SW. You deploy a new version that includes an updated SW with a cache busting version. The company then announces the new features. People visit the site, however, even though the SW busts it still serves up the previous cache while updating its cache in the background. So visitors that come for the new features don't see them.

Is this the expected experience with ServiceWorkers? What are the recommended strategies to get around this?

like image 695
bcardarella Avatar asked Apr 26 '17 17:04

bcardarella


People also ask

How do I bypass service worker?

To bypass the service worker, set ngsw-bypass as a request header, or as a query parameter. The value of the header or query parameter is ignored and can be empty or omitted.

How do I disable service worker cache?

Open the app in your browser (http://localhost:8443/). Turn off “Update on reload” if it is on (see “Service Worker Lifecycle”). Delete all existing service workers registered to this page. In Chrome, this can be achieved by using the “Clear storage” tool in the Application panel of the developer tools.

Where is Serviceworker cache stored?

Note that this is NOT the Cache folder. It's the Cache of Service Worker, the path should be AppData\Local\Microsoft\Edge\User Data\Default\Service Worker .

Should you cache the service worker?

Caching unnecessary resources wastes bandwidth and storage space and could cause your app to serve unintended outdated resources. You don't need to cache all the assets at once, you can cache assets many times during the lifecycle of your PWA, such as: On installation of the service worker. After the first page load.


1 Answers

It's the expected behavior whenever you serve resources with a cache-first strategy, yes.

There are two options:

  • Don't use a cache-first strategy. Unfortunately, you lose out on most of the performance benefits of service workers if you use a network-first strategy. I wouldn't recommend going network-first if you can help it.
  • Adopt the UX pattern of displaying a "Reload for the latest updates" toast message on the screen letting the user know that the cached content has been refreshed, and allowing them to take action to see the latest content. This is, I think, the best approach. If you're using a service worker which gets updated whenever your cached content changes (e.g., one generated by sw-precache), then you can detect these updates by listening for specific service worker controller events, and use those to trigger the message. (Here's an example.)
like image 122
Jeff Posnick Avatar answered Dec 31 '22 19:12

Jeff Posnick