How to update app when it's cached by a service worker?


#1

I’m building my first progressive web app, using a service worker to cache files for offline use.

My question: what happens when I need to deploy an updated version of my app? In my experiments so far, the user agent always loads the application from the service worker cache – even when the device is online.

Do I need to build some kind of check into my app? For example, at launch time, it could check a remote URL, which would return the latest version number. If the app finds that its own version number is out of date, then it would clear the service worker cache and…ask the user to reload or something?

Something like that? What’s the established pattern here?


#2

I’m no expert with service-workers, but I changed mine to:

self.toolbox.router.any('/*', self.toolbox.fastest);

which does the following:

toolbox.fastest
Request the resource from both the cache and the network in parallel. Respond with whichever returns first. Usually this will be the cached version, if there is one. On the one hand this strategy will always make a network request, even if the resource is cached. On the other hand, if/when the network request completes the cache is updated, so that future cache reads will be more up-to-date.