Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for service worker updates periodically

How do I poll the server every minute to check the service worker update?

I have used the available method of SWUpdate angular, but it doesn't poll the server, instead it fetched the updates and caches when the page is refreshed which is not required.

I came across checkForUpdate() method, which does quite similar, but not sure of how it works?

Any of the help would be appreciated.

ngOnInit(){

   if(this.swUpdate.isEnabled){
     this.swUpdate.available.subscribe( () => {

       if(confirm("New Version available.. Load?")){
         window.location.reload();
       }


     })
   }

 }

Above code which fetches the data from cache on the second reload.

like image 689
Jay Avatar asked Feb 08 '18 06:02

Jay


People also ask

How do I know if a service worker is working?

A: From a page on the same origin, go to Developer Tools > Application > Service Workers. You can also use chrome://inspect/#service-workers to find all running service workers.

How does a 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.

How do I keep my service worker running?

There is no way to prevent ServiceWorker from stopping. Instead, ServiceWorker automatically starts again when necessary, for example, when a push message arrives.


1 Answers

If it is still relevant you can do something like this: setInterval(function(){ swUpdate.checkForUpdate(); }, 1000);

This will check every second if there is a new version of you application. When it finds the new version it will go in the available once it got the new version downloaded.

Hope this might still help you and is what you are looking for.

like image 172
Joey Driessen Avatar answered Oct 17 '22 04:10

Joey Driessen