Sha256: 9000d89b628dd146ea838a882dc0e20b8902ed5ba909cdd9f2d4a3d33f3b4ede

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

---
layout: compress
permalink: '/sw.js'
# PWA service worker
---

self.importScripts('{{ "/assets/js/data/swcache.js" | relative_url }}');

const cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M%S" }}';

function verifyDomain(url) {
  for (const domain of allowedDomains) {
    const regex = RegExp(`^http(s)?:\/\/${domain}\/`);
    if (regex.test(url)) {
      return true;
    }
  }

  return false;
}

function isExcluded(url) {
  for (const item of denyUrls) {
    if (url === item) {
      return true;
    }
  }
  return false;
}

self.addEventListener('install', event => {
  event.waitUntil(
    caches.open(cacheName).then(cache => {
      return cache.addAll(resource);
    })
  );
});

self.addEventListener('activate', event => {
  event.waitUntil(
    caches.keys().then(keyList => {
      return Promise.all(
        keyList.map(key => {
          if (key !== cacheName) {
            return caches.delete(key);
          }
        })
      );
    })
  );
});

self.addEventListener('message', (event) => {
  if (event.data === 'SKIP_WAITING') {
    self.skipWaiting();
  }
});

self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request).then(response => {
      if (response) {
        return response;
      }

      return fetch(event.request).then(response => {
        const url = event.request.url;

        if (event.request.method !== 'GET' ||
              !verifyDomain(url) ||
              isExcluded(url)) {
          return response;
        }

        /*
          see: <https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests>
        */
        let responseToCache = response.clone();

        caches.open(cacheName).then(cache => {
          /* console.log('[sw] Caching new resource: ' + event.request.url); */
          cache.put(event.request, responseToCache);
        });

        return response;
      });
    })
  );
});

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
jekyll-theme-chirpy-clarkezone-5.3.2 assets/js/pwa/sw.js
jekyll-theme-chirpy-5.3.0 assets/js/pwa/sw.js
jekyll-theme-abd3lraouf-5.2.9 assets/js/pwa/sw.js
jekyll-theme-abd3lraouf-5.2.8 assets/js/pwa/sw.js
jekyll-theme-abd3lraouf-5.2.7 assets/js/pwa/sw.js
jekyll-theme-chirpy-5.2.1 assets/js/pwa/sw.js
jekyll-theme-chirpy-5.2.0 assets/js/pwa/sw.js