Skip to content

Instantly share code, notes, and snippets.

@Himachallad
Created July 26, 2020 19:42
Show Gist options
  • Select an option

  • Save Himachallad/8bfac63d24b9cffc67b47699198b13ac to your computer and use it in GitHub Desktop.

Select an option

Save Himachallad/8bfac63d24b9cffc67b47699198b13ac to your computer and use it in GitHub Desktop.

Revisions

  1. Himachallad created this gist Jul 26, 2020.
    23 changes: 23 additions & 0 deletions Activate service worker
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    /**
    * Activates the new service worker and clean old cache incase of version changes
    */
    self.addEventListener("activate", (event) => {
    const currentCaches = [UNVARYING_CACHE, VOLATILE];
    event.waitUntil(
    caches
    .keys()
    .then((cacheNames) => {
    return cacheNames.filter(
    (cacheName) => !currentCaches.includes(cacheName)
    );
    })
    .then((cachesToDelete) => {
    return Promise.all(
    cachesToDelete.map((cacheToDelete) => {
    return caches.delete(cacheToDelete);
    })
    );
    })
    .then(() => self.clients.claim())
    );
    });