Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for service worker updates in a single page app

We have a signal page application that has Service worker installed and active.

Now our server rebuilds the app, Service worker file is being updated on the server side.

Our client has no idea about this new Service worker file and it is still using old Service worker file.

When It works? if I refresh/reload the app, new service worker is being installed .

How can we refresh the client if service worker file is being updated on the server and client is not being reloaded?

Possible solution: I think,I need to do the polling after 1 hour or something.

like image 914
Sandeep Sharma Avatar asked Aug 08 '18 13:08

Sandeep Sharma


People also ask

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 does service worker detect new version?

The browser detects updates using a byte-by-byte comparison between the cached files and the resources coming from the network. Then the browser tries to install the new version of the service worker, and the new service worker will be in the waiting state, as described in the Service workers chapter.

What is use of serviceWorker?

Service workers are specialized JavaScript assets that act as proxies between web browsers and web servers. They aim to improve reliability by providing offline access, as well as boost page performance.


1 Answers

Implicit updates

"The Service Worker Lifecycle" is a great resource for these types of questions.

In particular, there's a section dealing with updates:

An update is triggered:

  • On navigation to an in-scope page.
  • On functional events such as push and sync, unless there's been an update check within the previous 24 hours.
  • On calling .register() only if the service worker URL has changed.

Explicit updates

Because your SPA doesn't use real navigations, you won't get the automatic update checks mentioned in the first bullet point.

What you can do instead is follow the example from later on in that section:

As I mentioned earlier, the browser checks for updates automatically after navigations and functional events, but you can also trigger them manually:

navigator.serviceWorker.register('/sw.js').then(reg => {
  // sometime later…
  reg.update();
});

What I'd probably do is to modify your SPA's router and automatically make a call to reg.update() whenever your router is about to switch to a new view. That would simulate the same update-on-navigation behavior that you'd get in a non-SPA.

How does register() fit in?

After at least one call to navigator.serviceWorker.register() has been made, the service worker has a chance to install and activate. At this point, checks for service worker updates will happen irrespective of whether or not navigator.serviceWorker.register() is called again.

The earlier sections of this answer describe when these update checks will take place, either implicitly or explicitly.

You can dive into the details of this by comparing the differences between the Register and Update jobs in the service worker specification.

like image 165
Jeff Posnick Avatar answered Oct 17 '22 19:10

Jeff Posnick