Reputation: 19391
e.g. we have a PWA application running for a year on a server and now we plan to stop the service (shutdown the server and remove the DNS entries)
But when we just shut down the server, the client-browsers will still have the PWA cached and the users will see error messages that the server cannot be reached, whenever they execute an action.
How can we handle this situation gracefully?
more details:
This site can’t be reached
Upvotes: 1
Views: 585
Reputation: 10830
Can you release a new web site version before shutting down the server?
In there you can not only delete the SW, but also cancel its cache:
// Unregister the SW
navigator.serviceWorker.getRegistration()
.then(function(registration) {
if(registration){
registration.unregister()
.then(
function(success) {
// if success = true, unregister was successful
});
}
});
// Delete SW Cache
if ('caches' in window) {
caches.keys()
.then(function(keyList) {
return Promise.all(keyList.map(function(key) {
return caches.delete(key);
}));
})
}
I wrote an article about Service Workers and caching strategies if you want to have a look a it.
Of course all the users that have already the SW on their side and won't visit your portal before shutting it down, will still have the old version, but for that I do not think you have much control.
UPDATE
I further checked some documentation, but I could not find anything about a timed lifespan (not even on the MDN docs). Therefore it seems not possible to define a time limit from within the SW.
The problem is that the service worker runs independently on its thread once installed on the client. Therefore you do not have control on it, unless by letting install a new SW. Maybe a possible solution would be to redirect the current URL address to a specific app that simply let download the new SW (that will uninstall it and remove the cache).
Upvotes: 4