Service worker: Fetch event listener only works after page reload

前端 未结 1 1747
心在旅途
心在旅途 2021-01-03 04:28

I have added a service worker to my page with the code below. It works well once the page has been reloaded and the worker already installed. But does not seem to catch any

相关标签:
1条回答
  • 2021-01-03 04:37

    Solution: Adding the following to worker.js;

    self.addEventListener('activate', function (event)
    {
        event.waitUntil(self.clients.claim());
    });
    

    Service workers don’t immediately “claim” the sessions that load them, meaning that until your user refreshes the page your service worker will be inactive.

    The reason for this is consistency, given that you might otherwise end up with half of your webpage’s assets cached and half uncached if a service worker were to come alive partway through your webpage’s initialization. If you don’t need this safeguard, you can call clients.claim and force your service worker to begin receiving events

    Read more @ service-workers

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