I have added PWA features and service worker to my angular website. After the release to the live server many user started getting issues, like: "This Site Can't Be Reached", but when user clicks CTRL+ F5 then it works fine.
For immediate solution I have removed the PWA and service worker from my release. Nevertheless many users are facing the same issue or "index page not found" error from service worker.
How can I remove or unregister the old service worker from the end user's browser?
I tried this code to remove the service worker cache :
if ('caches' in window) {
caches.keys()
.then(function(keyList) {
return Promise.all(keyList.map(function(key) {
return caches.delete(key);
}));
})
}
To remove the service worker I have added the following:
if(window.navigator && navigator.serviceWorker) {
navigator.serviceWorker.getRegistrations()
.then(function(registrations) {
for(let registration of registrations) {
registration.unregister();
}
});
}
Open the app in your browser (http://localhost:8443/). Turn off “Update on reload” if it is on (see “Service Worker Lifecycle”). Delete all existing service workers registered to this page. In Chrome, this can be achieved by using the “Clear storage” tool in the Application panel of the developer tools.
So my answer is: Yes, you can delete it from Explorer. If you are worried about safety, you may at first step move the contents to a temporary folder and run Microsoft Edge. If everything works well, you may then delete the temporary folder. Note: Google Chrome behaves in exactly the same way.
As default when installing Angular PWA, static content, such as assets, javascript and CSS files, will be cached to make the page load faster. You should always cache these because the anti-cache hashes in the file names will bust the cache on every deployment, making cache handling easy.
If you dont want your users to install the PWA, please disable only the PWA and keep the service worker running. We dont recommend disabling PWA as it does not take traffic away from your main resources and does not drain SEO.
In Online mode, Service worker is unable to create the "ngsw:xxxdataxxx" caches again, after they are removed once, but still, service worker is able to save the new api responses somewhere, and it is serving these responses in Offline mode. My guess is angular service worker uses some other memory in addition to cache storage.
PWA, progressive web apps, gives our browser native superpowers, such as offline availability and being able to install websites as a native app. The key to creating maintainable and performant offline apps is to handle caching without cluttering your code with complicated caching logic.
The code you used is correct to unregister the service worker and delete its cache from the client.
Is your SW processing any long run tasks? Because if so, it will wait to complete that task before being removed.
In the worst scenario, your service worker will check within 24 hours if a newer version is available. This feature is called fail safe and will be triggered by the browser.
Your code is already good, but if you want to read more about Service Workers and caching strategies, I wrote an article about them.
The browser will disable the service worker after its lifetime.
I have faced this issue once and it was really painful.
Hack: In your app.component.ts use the following line inside ngAfterViewInit();
ngAfterViewInit() {
window.location.reload();
}
This is a dirty hack not a bad practice keep this code live for few days until you know that your users have the latest version of your angular-app.
After that if you wish to use Service-Workers for PWA Use the following code to automatically update your client.
Inject
private swUpdate: SwUpdate, private snackbar: MatSnackBar
in constructor and write the following code inside it.
this.swUpdate.available.subscribe(evt => {
let snack = this.snackbar.open('Update Available', 'Reload Please.');
snack.onAction().subscribe(() => {
window.location.reload();
});
setTimeout(() => {
snack.dismiss();
}, 10000);
});
If you still face any problem let me know that can be an interesting problem to solve.
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