Service workers

Up till now, I have mostly ignored the activate event. This is a mistake. The activate event signals that the service worker code has been updated, and so I might want to invalidate the cache and repopulate it.

Other important notes

  • The page that calls register doesn't get the service worker! The page has to load through the service worker before it will use the service worker.
  • A new version of the service worker is installed if/when the fetched version is different
    • This implies that we should be careful loading the sw from sw cache
  • The new version doesn't start taking events until all pages using the old worker have closed
  • Refreshing the page doesn't count as closing! You must navigate away or close the tab. (Irritating from a debugging point of view)
  • force-reload (shift-reload?) bypasses the service worker

It looks like we're expected to use a different cache name with each different version of the sw script, and delete old caches from the activate event.

Todo: Work out how to programmatically set the cache version, ideally using either the hash of the sw code, or the slug of the timestamp.


To remember your current position in the blog, this page must store some data in this browser.

Are you OK with that?