1

In this answer When and how does a PWA update itself? it is explained that a PWA is updated if manifest.json links are updated or if one character is modified from the service-worker.js file.

But what do we exactly mean by "updated"? Does this mean the install event is re-triggered and that all the listed files below are re-downloaded?

Is there a documentation confirming this is the case for all browsers on Android and iOS? (I don't know if the latter uses PWA the same way than on Android)

Here is an example service-worker.js file:

self.addEventListener('install', (e) => {
  e.waitUntil(
    caches.open('myapp-store').then((cache) => cache.addAll([
      '/app/index.html',
      '/app/file1.jpg',   
      '/app/file2.mp3',   
      '/app/file3.png',   
    ])),
  );
});

self.addEventListener('fetch', (e) => {
  console.log(e.request.url);
  e.respondWith(
    caches.match(e.request).then((response) => response || fetch(e.request)),
  );
});
Basj
  • 41,386
  • 99
  • 383
  • 673

1 Answers1

2

The service worker update flow is independent from any update checks that apply to the web app manifest.

"The Service Worker Lifecycle" is recommended reading, and it explains the complain initial install and update flow.

To borrow from that document and answer your question, yes, the install event of the updated service worker file will be executed once a browser detects that update. If your updated service worker file includes a cache.addAll() then that will be executed, and if the URLs being cached are identical to the same URLs previously cached, and the cache name is the same, then the old cached values will be overwritten with whatever is fetche during install.

Many folks include inline versioning information in their service worker file so that they can trigger an update by changing that value. They also might use a new cache name tied to that versioning information, so that instead of overwriting previously cached values, new cache entries will be created. This tends to be safer, since the old service worker will remain in control of open pages until the updated service worker activates, and using versioned cache names means that the old service worker can continue reading the cached values it expects.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167