<commit_before>var CACHE = "cache-and-update9";
var CACHE_TYPES = [".json", ".js", ".css", ".svg"];
var OFFLINE_CACHE = "offline-cache";

self.addEventListener("install", function(evt) {

  // console.log("The service worker is being installed.");

  evt.waitUntil(precache());
});

self.addEventListener("fetch", function(evt) {
  if (evt.respondWith) {
    var serveCachedAsset = CACHE_TYPES.map(function(a){
      return evt.request.url.endsWith(a) || evt.request.url.endsWith(a+"?body=1");
      }).includes(true);

    if (serveCachedAsset) {
      // console.log("The service worker is serving the asset: "+evt.request.url);
      evt.respondWith(fromCache(evt.request));
      evt.waitUntil(update(evt.request));
    }
    else {
      var request = evt.request;
      var path = request.url.split("//")[1].split("/").slice(1,1000).join("/");

      if (request.method === "GET" && path != "heartbeat" && !path.match(/\.jpg(\?\d*)?$/)) {
        evt.respondWith(
          fetch(request).then(function(response){
            caches.open(CACHE).then(function(cache) {
              // console.log("Adding to cache: " + path)
              cache.put(request.url, response);
            });
            return response.clone();
          }).catch(function(error){
            console.log("[onfetch] Failed. Serving cached version" + error);
            var collectionId = path.match(/collections\/(\d+)/i);
            collectionId = collectionId ? collectionId[1] : null;
            var workId = path.match(/works\/([\da-z]+)/i);
            workId = workId ? workId[1] : null;

            return caches.open(CACHE).then(function(cache){
              if (path == "collections") {
                return cache.match("collections");
              } else if (workId == "new") {
                return cache.match("offline/work_form");
              } else if (collectionId) {
                return cache.match("offline/collection");
              } else {
                return cache.match("offline/offline");
              }
            });
          })
        );
      }
    }
  };
});

function precache() {
  return caches.open(CACHE).then(function (cache) {
    return cache.addAll([
      "./offline/offline",
      "./geoname_summaries.json",
      "./offline/work_form",
      "./offline/collection"
    ]);
  });
}

function fromNetwork(request, timeout) {
  return new Promise(function (fulfill, reject) {
    var timeoutId = setTimeout(reject, timeout);
    fetch(request).then(function (response) {
      clearTimeout(timeoutId);
      fulfill(response);
    }, reject);
  });
}

function fromCache(request) {
  return caches.open(CACHE).then(function (cache) {
    return cache.match(request).then(function (matching) {
      return matching || Promise.reject("no-match");
    }).catch(function() {
      return fromNetwork(request, 10000)
    });
  });
}

function update(request) {
  return caches.open(CACHE).then(function (cache) {
    return fetch(request).then(function (response) {
      return cache.put(request, response);
    });
  });
}
<commit_msg>cache fix?<commit_after>var CACHE = "cache-and-update10";
var CACHE_TYPES = [".json", ".js", ".css", ".svg"];
var OFFLINE_CACHE = "offline-cache";

self.addEventListener("install", function(evt) {

  // console.log("The service worker is being installed.");

  evt.waitUntil(precache());
});

//clean!
self.addEventListener('activate', function(event) {
  event.waitUntil(clearOtherCaches());
});

self.addEventListener("fetch", function(evt) {
  if (evt.respondWith) {
    var serveCachedAsset = CACHE_TYPES.map(function(a){
      return evt.request.url.endsWith(a) || evt.request.url.endsWith(a+"?body=1");
    }).includes(true);

    if (serveCachedAsset) {
      // because of how rails caching works, can be served from cache without a thought
      evt.respondWith(fromCache(evt.request));
      evt.waitUntil(update(evt.request));
    }
    else {
      var request = evt.request;
      var path = request.url.split("//")[1].split("/").slice(1,1000).join("/");

      if (request.method === "GET" && !path.match(/\.jpg(\?\d*)?$/) && !path.match(/collections\/(\d+)\/works/i)) {
        evt.respondWith(
          fetch(request).then(function(response){
            caches.open(CACHE).then(function(cache) {
              // console.log("Adding to cache: " + path)
              cache.put(request.url, response);
            });
            return response.clone();
          }).catch(function(error){
            console.log("[onfetch] Failed. Serving cached version" + error);
            var collectionId = path.match(/collections\/(\d+)/i);
            collectionId = collectionId ? collectionId[1] : null;
            var workId = path.match(/works\/([\da-z]+)/i);
            workId = workId ? workId[1] : null;

            return caches.open(CACHE).then(function(cache){
              if (path == "collections") {
                return cache.match("collections");
              } else if (workId == "new") {
                return cache.match("offline/work_form");
              } else if (collectionId) {
                return cache.match("offline/collection");
              } else {
                return cache.match("offline/offline");
              }
            });
          })
        );
      }
    }
  };
});

function precache() {
  return caches.open(CACHE).then(function (cache) {
    return cache.addAll([
      "./offline/offline",
      "./geoname_summaries.json",
      "./offline/work_form",
      "./offline/collection"
    ]);
  });
}

function fromNetwork(request, timeout) {
  return new Promise(function (fulfill, reject) {
    var timeoutId = setTimeout(reject, timeout);
    fetch(request).then(function (response) {
      clearTimeout(timeoutId);
      fulfill(response);
    }, reject);
  });
}

function fromCache(request) {
  return caches.open(CACHE).then(function (cache) {
    return cache.match(request).then(function (matching) {
      return matching || Promise.reject("no-match");
    }).catch(function() {
      return fromNetwork(request, 10000)
    });
  });
}

function update(request) {
  return caches.open(CACHE).then(function (cache) {
    return fetch(request).then(function (response) {
      return cache.put(request, response);
    });
  });
}

function clearOtherCaches() {
  caches.keys().then(function(cacheNames) {
    return Promise.all(
      cacheNames.filter(function(cacheName) {
        return cacheName != CACHE
      }).map(function(cacheName) {
        return caches.delete(cacheName);
      })
    );
  })
}

{'fraction_non_alphanumeric': 0.1668696711327649, 'fraction_numerical': 0.0047198538367844095, 'mean_word_length': 2.520364415862808, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6260523', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1978, 'n_words': 350}