18

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.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
Sandeep Sharma
  • 1,855
  • 3
  • 19
  • 34
  • Do you deliver the SW.js file with HTTP-Cache headers? I think it should try to update (request from server) on every page load, but maybe it still gets the old version from browser cache. See https://stackoverflow.com/a/38854905/7362396 – Tobias K. Aug 09 '18 at 05:45
  • it is a single page app, page only gets loaded only when we manually refresh or open a new tab. – Sandeep Sharma Aug 09 '18 at 06:03

1 Answers1

30

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.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • 3
    Thanks for explaining this. My 6 months of struggle has been answered by this reply. I added a history.listener to check for service worker updates. I implemented the flow to ask user permission to reload app for getting latest updates, across multiple tabs. You can check my code https://github.com/rahuls360/testing-sw – Rahul Makhija Sep 13 '20 at 16:14