Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 PWA Service Worker doesn't update when new updates available

After i update my Angular 4 PWA app and deployed , user not getting new updates until user clear the cache and refresh the browser. Sw doesn't update. Even i press the update button in crome Dev it doesn't update I have to clear the cache and refresh the browser.

I used these packages @angular/service-worker @angular/platform-server ng-pwa-tools

like image 352
shashik thiwanka Avatar asked Sep 12 '17 15:09

shashik thiwanka


People also ask

How do I update my service worker?

update() The update() method of the ServiceWorkerRegistration interface attempts to update the service worker. It fetches the worker's script URL, and if the new worker is not byte-by-byte identical to the current worker, it installs the new worker.

Does PWA use service worker?

Service workers are a fundamental part of a PWA. They enable fast loading (regardless of the network), offline access, push notifications, and other capabilities.

How does PWA update?

In essence, there are only two events that will automatically trigger a PWA update: A change in the linked manifest. json ; e.g. a changed icon file or a changed scope or start_url . A one-character change in the controlling service-worker.

Which of the given operations are supported by the SwUpdate service?

The SwUpdate service supports three separate operations: Get notified when an updated version is detected on the server, installed and ready to be used locally or when an installation fails. Ask the service worker to check the server for new updates.


2 Answers

Make sure your app checks for updates from the server from time to time.

In the app.component.ts add private swUpdate: SwUpdate into the constructor.

 this.swUpdate.available.subscribe(event => {
   if (confirm('Update Available. Refresh the page now to update the cache.')) {
     location.reload(true);
   } else {
     console.log('continue with the older version');
   }
 });

 setInterval(() =>  {
   this.swUpdate.checkForUpdate();
 }, 21600);
like image 105
Dilshan Liyanage Avatar answered Oct 18 '22 19:10

Dilshan Liyanage


Based on the information you've provided I guess your web server is serving the SW file with some caching headers and the visitors' browsers use the cached version.

Be sure to explicitly set the caching headers to no-cache/-1/etc. so that the browser always checks the web server for a new version of the service-worker.js (or whatever) file.

like image 41
pate Avatar answered Oct 18 '22 19:10

pate