1

Short: I wonder whether it is possible to trigger some kind of event or callback when the serviceworker finished downloading files after an update.

Long version: I have a PWA using angular(8) and its angular serviceworker. In the ngsw config I put the install and update mode to prefetch.

Normally when an update is pushed to the server, the application registers to the update event and the serviceworker starts downloading the new files (.js, assets, .html etc). I can see in the network tab of the console how the new files are downloaded one by one. However since it is a large app, this can take several minutes.

Since my PWA is used a lot in offline mode, it is fatal if the update was not completed before the user closed the app or lost internet connection, because he might miss some scripts or pages. I would like to tell the user, when files are still downloading or at least when the file download has completed.

Is there some kind of callback, hook or whatever that gets triggered when the serviceworker finishes downloading stuff? Or maybe some other form to acchieve this?

Chris
  • 4,238
  • 4
  • 28
  • 49

1 Answers1

0

Yes, Angular provides a SwUpdate service you can subscribe to. It's usage looks like this:

@Injectable()
export class LogUpdateService {

  constructor(updates: SwUpdate) {
    updates.available.subscribe(event => {
      console.log('current version is', event.current);
      console.log('available version is', event.available);
    });
    updates.activated.subscribe(event => {
      console.log('old version was', event.previous);
      console.log('new version is', event.current);
    });
  }
}
abraham
  • 46,583
  • 10
  • 100
  • 152
  • Thanks, I am aware of that. But that is called at the beginning of the update, no? However I am looking for detecting the completion of it - or better - the end of the fetch of all new files. – Chris Feb 27 '20 at 13:29
  • You should test that it works for you but based on the description `available` should trigger when the new service worker is installed and precaching is completed. "Getting notified of available updates. These are new versions of the app to be loaded if the page is refreshed." – abraham Feb 27 '20 at 13:46
  • 1
    After some initial test, I think you might be right. I will accept your answer and in case I find some more information update it. Meanwhile another idea seems to be integrating this one: https://stackoverflow.com/a/49579418/3719922 – Chris Feb 27 '20 at 15:55
  • This seems to indicate only that a new version is available. It does not seem to indicate when the load of the initial serviceworker and assets is complete. I'm really interested in informing users when the app is ready to use offline in the first instance. – Mark Feb 03 '22 at 00:13
  • My requirement is also same as Mark ==> How to know if all app assets has been downloaded in the first instance – Sourav May 19 '22 at 07:45