3

I'm having an Angular PWA application which checks for a service worker updates every 15 seconds, meaning the app sees if the cached static files are still valid. If there was a new deployment the service worker will silently update the cache and let the user know that there are updates available and if he/she wants to refresh.

This scenario is alright if the user is currently using the application. But let's say if the user is not using the application. Is it possible to update the cache even when the browser is closed? I am thinking this might be possible because once the service worker is registered it can listen to push notifications so is it possible to automatically update the cache of an application as well?

Dilshan Liyanage
  • 4,440
  • 2
  • 31
  • 33

2 Answers2

1

In general when the browser is closed (not running) you app can not do anything including get push notifications. I believe Android is an exception to this as the device handles push notifications and will then start the browser to handle the notification.

When the browser is running but no tabs/windows are open to your site, your service worker can not trigger any activity. There have been some proposals for APIs but they all have a lot of privacy issues and haven't gone anywhere.

In theory you could send push notifications to wake up the service worker and check of updates but there are quotas that limit the number of total pushes. Most if not all browsers also require that web pushes show a notification to the user to prevent silent user tracking.

Also note that checking for updates every 15 seconds is pretty aggressive. You should make sure that you are not adversely draining device resources and consuming bandwidth. You might want to look into web sockets or server sent events. This would all your server to notify active clients of updates.

As for keeping your content as current as possible I would suggest trying to architect your app for hot swapping code. So you can load the cached version of the site, check for updates and seamlessly start running the updated code in the active client without having to reload.

abraham
  • 46,583
  • 10
  • 100
  • 152
  • Thanks Abraham, this is the first time I have heard about server-sent events and will have to look into it and the 15 seconds thing is not that much of a problem because its an enterprise sort of an app only used by internal employees and its never opened on the mobile. It's always the desktop and connection is only on LAN. – Dilshan Liyanage Mar 11 '19 at 18:30
  • 1
    I would suggest going with a stream of some sort as that would also avoid the 15 second delay between polls. – abraham Mar 11 '19 at 18:37
0

If you want your service worker to be updated, you should use its update capabilities - SwUpdate from '@angular/service-worker'.

Subscribing to SwUpdate notify you when an update is available, allowing you to decide what to do about it.

Please find more info here

Noam Gal
  • 1,124
  • 1
  • 11
  • 15