I have an Angular application (version 7.2.2) using the Angular Service Worker (version 7.2.12).
The service worker intermittently serves an old version of the application. On refresh the new version is shown, but if the user closes and reopens the browser tab, it reverts back to the old version until the next page reload. The ‘/ngsw/state’ page also has no errors when the old version is displayed.
I have not been successful in consistently reproducing the issue. In part because I am unsure of the cause. I have often gone weeks without having the service worker serve an old version of the code.
Some of the potential solutions I tried which seemed promising include:
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.
ngsw-cache-bust=0.7064947087867681. It's trying to get the service worker configuration to see what needs updated, but can't access the file. However, this should not impede the service worker from serving up anything it's already cached, and operate normally until back online.
Adding a service worker to an Angular application is one of the steps for turning an application into a Progressive Web App (also known as a PWA). At its simplest, a service worker is a script that runs in the web browser and manages caching for an application. Service workers function as a network proxy.
The ngsw-config.json configuration file specifies which files and data URLs the Angular service worker should cache and how it should update the cached files and data. The Angular CLI processes the configuration file during ng build .
You may want to check out the details here. In short, be aware that service worker unregistering is slow and done in the background. For quick round trip at the developer stage, you may need to manually unregister and then reload.
Check out the information at the following location: https://love2dev.com/blog/how-to-uninstall-a-service-worker/
The original GIST points out we should clean down our service worker before starting up:
self.addEventListener("activate", function(e) {
self.registration.unregister()
.then(function() {
return self.clients.matchAll();
})
.then(function(clients) {
clients.forEach(client => client.navigate(client.url));
});
});
However the post suggests using this enhanced version:
self.addEventListener("activate", function(e) {
self.registration.unregister()
.then(function() {
return self.clients.matchAll();
})
.then(function(clients) {
clients.forEach(client => {
if (client.url && "navigate" in client){
client.navigate(client.url))
}
});
});
I would also ensure that:
I hope some of these suggestions get you moving in the right direction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With