w3c / ServiceWorker

Service Workers
https://w3c.github.io/ServiceWorker/
Other
3.63k stars 314 forks source link

Consider exposing Page Lifecycle state to service-worker #1345

Open spanicker opened 5 years ago

spanicker commented 5 years ago

Page lifecycle state could be exposed on WindowClient (similar to visibilityState): in particular Frozen and Discarded states. We need concrete use-cases for these: please comment here, if you have any.

One possible case is (new API for) updating tab title from service worker (eg. inbox count in gmail). While currently such sites needing updating of title / playing a sound (chat notification) etc don't get frozen, in the future there could be an API to support this via service worker. Such an API may need to know page lifecycle state.

jsaterfiel commented 5 years ago

I can see a use case for when you are using service workers as an alternative to sending pixel heart beats and want a more reliable way of knowing when a window has closed instead of using a timeout in the sw to know when a window has stopped sending data to it. Also helpful for knowing when windows have been backgrounded which changes how the window functions (timeout min becomes 1 sec, etc) but not the service worker.

rejhgadellaa commented 5 years ago

+1 and /sub

rejhgadellaa commented 5 years ago

I commented the scenario below regarding a discussion on the Chromium bugtracker:

My PWA has a notification click listener in its SW. When it is fired, it checks clients.matchAll (which, right now, seems to return clients even if they are frozen), sends a message to the first one it finds (using client.postMessage()) and then focuses it (client.focus()). If it doesn't find a client, it calls clients.openWindow().

Currently, the message gets stuck sometimes on Android and neither MessageChannel().port2.onmessage or -onmessageerror is ever fired. I suspect this is has to do with the tab/window being frozen.

My question/point is this: if clients.matchAll() would not return the frozen client(s), what would happen if I call clients.openWindow()? I would expect the SW to be able to open an existing client even if it's frozen. The user did leave it open, probably for a reason. Recycling the client makes sense?

In the case of a PWA it's probably less of a problem since I assume it would just use the existing application window and (re)load. But when it's a regular tab, clients.openWindow() would open a new tab even if there is an existing tab.

In short, it would help if I could determine if a client was frozen so the SW can at least act accordingly.

See https://bugs.chromium.org/p/chromium/issues/detail?id=957597#c11