---
layout: compress
# PWA service worker
---

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

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

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', e => {
  self.skipWaiting();
  e.waitUntil(
    caches.open(cacheName).then(cache => {
      return cache.addAll(resource);
    })
  );
});

self.addEventListener('fetch', e => {
  e.respondWith(
    caches.match(e.request).then(r => {
      /* console.log(`[sw] method: ${e.request.method}, fetching: ${e.request.url}`); */
      return r || fetch(e.request).then(response => {
        const url = e.request.url;

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

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

      });
    })
  );
});

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

@@ -712,24 +712,28 @@
 r('fetch', e
+vent
  =%3E %7B%0A  e.re
@@ -729,16 +729,20 @@
 =%3E %7B%0A  e
+vent
 .respond
@@ -761,24 +761,28 @@
 ches.match(e
+vent
 .request).th
@@ -782,110 +782,99 @@
 est)
-.then(r =%3E %7B%0A      /* console.log(%60%5Bsw%5D method: $%7Be.request.method%7D, fetching: $%7Be.request.url%7D%60); */%0A
+%0A      .then(response =%3E %7B%0A        if (response) %7B%0A          return response;%0A        %7D%0A%0A  
     
@@ -885,21 +885,16 @@
 turn
- r %7C%7C
  fetch(e
 .req
@@ -889,16 +889,20 @@
  fetch(e
+vent
 .request
@@ -902,16 +902,27 @@
 request)
+%0A          
 .then(re
@@ -937,24 +937,28 @@
 %3E %7B%0A        
+  
+  
 const url = 
@@ -958,16 +958,20 @@
  url = e
+vent
 .request
@@ -989,13 +989,21 @@
     
+ 
+   
 if (e
+vent
 .req
@@ -1023,16 +1023,19 @@
 == 'GET'
+ %7C%7C
 %0A       
@@ -1037,18 +1037,19 @@
         
-%7C%7C
+   
  !verify
@@ -1059,16 +1059,19 @@
 ain(url)
+ %7C%7C
 %0A       
@@ -1073,18 +1073,19 @@
         
-%7C%7C
+   
  isExclu
@@ -1098,32 +1098,36 @@
 l)) %7B%0A          
+    
 return response;
@@ -1127,32 +1127,36 @@
 sponse;%0A        
+  
+  
 %7D%0A%0A        retur
@@ -1142,38 +1142,237 @@
      %7D%0A%0A        
-return
+    /*%0A              see: %3Chttps://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests%3E%0A             */%0A            let responseToCache = response.clone();%0A%0A           
  caches.open(cac
@@ -1370,32 +1370,47 @@
 .open(cacheName)
+%0A              
 .then(cache =%3E %7B
@@ -1420,16 +1420,22 @@
         
+ 
+     
 /* conso
@@ -1474,16 +1474,20 @@
 e: ' + e
+vent
 .request
@@ -1492,24 +1492,30 @@
 st.url); */%0A
+      
           ca
@@ -1523,16 +1523,20 @@
 he.put(e
+vent
 .request
@@ -1549,19 +1549,39 @@
 onse
-.clone());%0A
+ToCache);%0A              %7D);%0A%0A  
     
@@ -1615,21 +1615,22 @@
     
+  
 %7D);%0A
-%0A
       %7D)
 ;%0A  
@@ -1625,25 +1625,19 @@
       %7D)
-;%0A    %7D)%0A
+%0A  
   );%0A%7D);

Fix the response stream of service worker