serviceworkers focus tab: clients is empty on notificationclick

后端 未结 2 788
渐次进展
渐次进展 2020-12-17 09:59

I have a common serviceworker escenario, where I want catch a notification click and focus the tab where the notification has come from. However, clients variable is always

相关标签:
2条回答
  • 2020-12-17 10:22

    Try making the service worker immediately claim the page. E.g.:

    self.addEventListener('install', event => event.waitUntil(self.skipWaiting()));
    
    self.addEventListener('activate', event => event.waitUntil(self.clients.claim()));
    

    For a more complex example, see https://serviceworke.rs/immediate-claim.html.

    0 讨论(0)
  • 2020-12-17 10:34

    The suspicion in your own comment is correct. A page is controlled by a service worker on navigation to an origin that the service worker is registered for. So the original page load that actually initializes the service worker is not itself controlled. That's why the worker only finds your tab once you visit with a new tab or do a refresh.

    However (as Jeff Posnick points out in the comments) you can get uncontrolled pages as follows: ServiceWorkerClients.matchAll({includeUncontrolled: true, type: 'window'}).

    0 讨论(0)
提交回复
热议问题