1

We're using workbox. We programmatically refresh the page if the user opens the installed PWA and there's a new version available.

It would be a better user experience if we do that while the app is in the background on user's device.

I haven't found any reference to that on google and SO.

Are there any examples of this? If I will make one, would anyone find it useful?

I'm spuprised it's not the de facto approach these days, but maybe there's something I'm missing here ...

I'm thinking of the following approach:

  1. Service worker is polling from server to check if a new version is available (or connects through WebSocket)

  2. If new version is found, service worker checks if the app is active/in the foreground. If it is, notify user about the new version, and offer a refresh for updates. If not, programtically make the reload, and restore the state of the app on load.

I would appreciate any thoughts and pointers on this, and if someone knows of a better approach for this.

Shining Love Star
  • 5,734
  • 5
  • 39
  • 49

2 Answers2

2

Following Jeff's great answer on another SO thread, seems like the way to go is with the Push API.

I might post here the code once I'm done, if there's a demand for it.

NOTE: ATM Safari and android webview do not support push api.

Shining Love Star
  • 5,734
  • 5
  • 39
  • 49
0

This is not possible. You cannot implement update checking that happens in the background in the Service Worker. Reason: to prevent abuse and save battery.

Some considerations:

  • WebSockets don't exist in SWs
  • SWs are killed in the background and they cannot be kept around with setTimeout/setInterval
  • Push messages from the server cannot be received silently

The last point means that you cannot make the server contact the SW of the app and have it silently do some stuff without showing a notification to the user.

pate
  • 4,937
  • 1
  • 19
  • 25
  • seems like [you are partialy right](https://stackoverflow.com/a/29750936/1226072), but it's possible through [Push API](https://developer.mozilla.org/en-US/docs/Web/API/Push_API) – Shining Love Star Jun 29 '19 at 07:30
  • 1
    @goldylucks yes, as I pointed out in my answer, right :) I listed it under the considerations since silent background pushes are not possible – but if you're ok with displaying a notification to the user, then sure, go ahead! – pate Jun 29 '19 at 09:38