self.skipWaiting() not working in Service Worker

后端 未结 1 1669
粉色の甜心
粉色の甜心 2021-02-09 18:23

I have a service worker. Here\'s the install event:

self.addEventListener(\'install\', function (event) {
    console.log(\'Installing Service Worker ...\', even         


        
相关标签:
1条回答
  • 2021-02-09 18:55

    The old SW might not stop while it's still running tasks - for example if it had a long running fetch request (e.g server sent events / event source, or fetch streams; although I don't think websockets can cause this as SW will ignore them I think).

    I find the behaviour to be different between browers however. Chrome seems to wait while the existing task is running (so skipWaiting will fail...), but Safari seems to kill the task and activate the new SW.

    A good way to test if this is causing your issue would be to kill your server just after you request the skipWaiting (to kill the network connections). (Just clicking "Offline" in Dev Tools doesn't seem to kill all running connections, for example EventSources stay running.)

    You can have the SW ignore certain routes (below), or you could try and force the requests to terminate (maybe using AbortController).

    self.addEventListener('fetch', function(event) {
      const { method, url } = event.request;
      if(event.request.method !== "GET") return false;
      if(url === "https://example.com/poll") return false;
    
      event.respondWith(
        caches.match(match).then(function(response) {
          return response || fetch(event.request);
        })
      );
    });
    

    The process for skipWaiting is in this spec:

    https://w3c.github.io/ServiceWorker/#try-activate-algorithm

    But I don't find it very clear about whether the browser should wait for tasks or terminate them (or transfer them to the new SW?), before activating the new SW; and as mentioned, it seems to work differently between browsers at the moment...

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