0

I have been trying to fetch and update the service worker without having the user interaction. I was able to cache the pagaes, but only upon refresh I was able to fetch the updates build, which requires user interaction. Any help on how could it be done.

Choice of framework: Angular2, sw-precache(Library)

Here is my script in top level

<script>
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/service-worker.js').then(function(registration) {
      console.log('Service Worker registered');
    }).catch(function(err) {
      console.log('Service Worker registration failed: ', err);
    });
  }
</script>
Jay
  • 473
  • 1
  • 6
  • 19

1 Answers1

0

sw-precache will update caches each time there's a change detected in the service worker file that you host on your web server. This Stack Overflow answer details how often these checks for new service worker files take place, but the basic idea is that the usual time that they happen is after a user takes action to navigate to a new URL under your service worker's scope.

While there are proposals for "periodic background sync" that would allow you to specifically request that a browser check for updates "in the background", without any use interaction, that hasn't been implemented anywhere yet. There are questions about battery life and using battery resources that need to be solved first.

It is possible to use push notifications to "wake up" a service worker without user interaction, and when the service worker starts running via a push event, it will also check for updates. You should show a visible notification in response to a push event, so while you can trigger one without user interaction, you will still end up needing to tell the user via the notification that a refresh request was made.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • Thanks for your concern, I somehow figured and resolved my issue a while back. :) – Jay Feb 14 '18 at 07:30