简体   繁体   中英

Adding activate event listener to service worker

I have a service worker I am trying to setup for my website. It uses basic html, and css. I need it to cache the files for offline mode. I currently am stuck at adding the activate event listener, and setting up my fetch function. How do I intergrate that, and am I missing anything greater? Thanks.

LINK - https://jsfiddle.net/scgdhusk/2/

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request)
  );
});

Well... adding the activate event listener is very simple:

self.addEventListener('activate', event => {
  // do whatever you want
});

However, since your question is so simple I take it that you haven't really read through all the recommended documentation but just started to hack something together. I really advice you to read Google's introduction to Service Workers. There are many, many footguns that you can avoid if you read the material.

:)

https://developers.google.com/web/fundamentals/primers/service-workers/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM