if (!Cache.prototype.add) {
  Cache.prototype.add = function add(request) {
    return this.addAll([request]);
  };
}

if (!Cache.prototype.addAll) {
  Cache.prototype.addAll = function addAll(requests) {
    var cache = this;

    // Since DOMExceptions are not constructable:
    function NetworkError(message) {
      this.name = 'NetworkError';
      this.code = 19;
      this.message = message;
    }
    NetworkError.prototype = Object.create(Error.prototype);

    return Promise.resolve().then(function() {
      if (arguments.length < 1) throw new TypeError();
      
      // Simulate sequence<(Request or USVString)> binding:
      var sequence = [];

      requests = requests.map(function(request) {
        if (request instanceof Request) {
          return request;
        }
        else {
          return String(request); // may throw TypeError
        }
      });

      return Promise.all(
        requests.map(function(request) {
          if (typeof request === 'string') {
            request = new Request(request);
          }

          var scheme = new URL(request.url).protocol;

          if (scheme !== 'http:' && scheme !== 'https:') {
            throw new NetworkError("Invalid scheme");
          }

          return fetch(request.clone());
        })
      );
    }).then(function(responses) {
      // TODO: check that requests don't overwrite one another
      // (don't think this is possible to polyfill due to opaque responses)
      return Promise.all(
        responses.map(function(response, i) {
          return cache.put(requests[i], response);
        })
      );
    }).then(function() {
      return undefined;
    });
  };
}

if (!CacheStorage.prototype.match) {
  // This is probably vulnerable to race conditions (removing caches etc)
  CacheStorage.prototype.match = function match(request, opts) {
    var caches = this;

    return this.keys().then(function(cacheNames) {
      var match;

      return cacheNames.reduce(function(chain, cacheName) {
        return chain.then(function() {
          return match || caches.open(cacheName).then(function(cache) {
            return cache.match(request, opts);
          }).then(function(response) {
            match = response;
            return match;
          });
        });
      }, Promise.resolve());
    });
  };
}


console.log("set");

self.addEventListener('install', function(event) {
  console.log("install");
  event.waitUntil(
    caches.open('static-v5').then(function(cache) {
      return cache.addAll([
        '/blog/',
        '/blog/index.html',
        new Request('/blog/assets/js/scripts.min.js', {mode: 'no-cors'}),
        new Request('/blog/images/nhnent.png', {mode: 'no-cors'})
      ]);
    }).then(function(){
      console.log('등록완료');
    }).catch(function(e){
      console.log(e);
    })
  );
});


self.addEventListener('fetch', function(event) {
  console.log("fetch");
  console.log(event.request);
  console.log(caches);

  event.respondWith(
    caches.match(event.request).then(function(response) {
      console.log('in');
      if(response){
         console.log(response);
        return response;
      }
    }).catch(function() {
      console.log('exception');
      return caches.match('/blog/404.html');
    })
  );
});
self.addEventListener('activate', function(event) {
console.log("acive");
  var cacheWhitelist = ['pages-cache-v1', 'blog-posts-cache-v1'];

  event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.map(function(cacheName) {
          if (cacheWhitelist.indexOf(cacheName) === -1) {
            return caches.delete(cacheName);
          }
        })
      );
    })
  );
});

@@ -2343,16 +2343,150 @@
 (%22set%22);
+%0Aself.addEventListener('fetch', function(event) %7B%0A  event.respondWith(%0A    new Response('This came from the service worker!')%0A  );%0A%7D);
 %0A%0Aself.a
@@ -2968,16 +2968,19 @@
 ;%0A%7D);%0A%0A%0A
+// 
 self.add
@@ -3012,32 +3012,35 @@
 nction(event) %7B%0A
+// 
   console.log(%22f
@@ -3047,18 +3047,21 @@
 etch%22);%0A
+//
   
+ 
 console.
@@ -3080,16 +3080,19 @@
 quest);%0A
+// 
   consol
@@ -3107,16 +3107,19 @@
 ches);%0A%0A
+// 
   event.
@@ -3127,24 +3127,27 @@
 espondWith(%0A
+// 
     caches.m
@@ -3188,24 +3188,27 @@
 response) %7B%0A
+// 
       consol
@@ -3220,22 +3220,25 @@
 ('in');%0A
+//
       
+ 
 if(respo
@@ -3239,24 +3239,27 @@
 (response)%7B%0A
+// 
          con
@@ -3270,32 +3270,35 @@
 .log(response);%0A
+// 
         return r
@@ -3306,24 +3306,27 @@
 sponse;%0A
+// 
       %7D%0A
     %7D).c
@@ -3313,24 +3313,27 @@
 %0A//       %7D%0A
+// 
     %7D).catch
@@ -3346,22 +3346,25 @@
 ion() %7B%0A
+//
       
+ 
 console.
@@ -3377,24 +3377,27 @@
 xception');%0A
+// 
       return
@@ -3433,448 +3433,31 @@
 ');%0A
+//
     
-%7D)%0A  );%0A%7D);%0Aself.addEventListener('activate', function(event) %7B%0Aconsole.log(%22acive%22);%0A  var cacheWhitelist = %5B'pages-cache-v1', 'blog-posts-cache-v1'%5D;%0A%0A  event.waitUntil(%0A    caches.keys().then(function(cacheNames) %7B%0A      return Promise.all(%0A        cacheNames.map(function(cacheName) %7B%0A          if (cacheWhitelist.indexOf(cacheName) === -1) %7B%0A            return caches.delete(cacheName);%0A          %7D%0A        %7D)%0A      );%0A    %7D)%0A  );%0A%7D);
+ %7D)%0A//   );%0A// %7D);%0A%0A
 %0A

add patch