<commit_before>importScripts('sw-toolbox.js');

toolbox.router.any('/', toolbox.networkFirst);

var urlsToPrefetch = [
  'assets/blog.css',
  'assets/blog.js',
  'assets/vendor.css',
  'assets/vendor.js',
  'vendor/Museo_Slab_500_2-webfont.woff',
  'vendor/Museo_Slab_500_2-webfont.woff2'
];

urlsToPrefetch.forEach(function (url) {
  toolbox.router.any(url, toolbox.fastest)
});
toolbox.precache(urlsToPrefetch);

var CURRENT_VERSION = 'v2';
var CACHE_URLS = {
  'https://api.cosmicjs.com/v1/blog-cb/object-type/posts': 1,
  'https://api.cosmicjs.com/v1/blog-cb/object-type/posts?bustcache=true': 2
};

function cacheOpen (event, response) {
  return function (cache) {
    cache
      .put(event.request, response.clone())
      .catch(function (err) {
        console.error('cache error', err);
      });
    return response;
  }
}

function fetchSuccess (event) {
  return function (response) {
    // console.info('fetch success', event.request.url);
    return caches
      .open(CURRENT_VERSION)
      .then(cacheOpen(event, response));
  }
}

function noCache (event) {
  return function () {
    console.info('no cache', event.request.url);
    return fetch(event.request)
      .then(fetchSuccess(event));
  }
}

function checkCacheResponse (response) {
  if (!response) {
    console.info('no cache response');
    throw new Error();
  }
  // console.info('cache response', response);
  return response;
}

this.addEventListener('fetch', function (event) {
  var cacheType = CACHE_URLS[event.request.url];
  if (!cacheType) return;

  // console.info('fetch', event.request.url);
  if (cacheType === 1) {
    event.respondWith(
      caches
        .match(event.request)
        .then(checkCacheResponse)
        .catch(noCache(event))
        .catch(function noCacheFail () {
          console.error('no cache fail');
        })
    );
  } else if (navigator.onLine) {
    var bustRequest = new Request('https://api.cosmicjs.com/v1/blog-cb/objects');
    fetch(bustRequest).then(fetchSuccess({
      request: bustRequest
    }));
  }
});

this.addEventListener('activate', function (event) {
  event.waitUntil(
    caches.keys().then(function (keyList) {
      return Promise.all(keyList.map(function (key) {
        if (key !== CURRENT_VERSION) {
          return caches.delete(key);
        }
      }));<commit_msg>Fix up URL<commit_after>importScripts('sw-toolbox.js');

toolbox.router.any('/', toolbox.networkFirst);

var urlsToPrefetch = [
  'assets/blog.css',
  'assets/blog.js',
  'assets/vendor.css',
  'assets/vendor.js',
  'vendor/Museo_Slab_500_2-webfont.woff',
  'vendor/Museo_Slab_500_2-webfont.woff2'
];

urlsToPrefetch.forEach(function (url) {
  toolbox.router.any(url, toolbox.fastest)
});
toolbox.precache(urlsToPrefetch);

var CURRENT_VERSION = 'v2';
var BLOG_POSTS = 'https://api.cosmicjs.com/v1/blog-cb/object-type/posts';

var CACHE_URLS = {
  'https://api.cosmicjs.com/v1/blog-cb/object-type/posts?bustcache=true': 2
};
CACHE_URLS[BLOG_POSTS] = 1;

function cacheOpen (event, response) {
  return function (cache) {
    cache
      .put(event.request, response.clone())
      .catch(function (err) {
        console.error('cache error', err);
      });
    return response;
  }
}

function fetchSuccess (event) {
  return function (response) {
    // console.info('fetch success', event.request.url);
    return caches
      .open(CURRENT_VERSION)
      .then(cacheOpen(event, response));
  }
}

function noCache (event) {
  return function () {
    console.info('no cache', event.request.url);
    return fetch(event.request)
      .then(fetchSuccess(event));
  }
}

function checkCacheResponse (response) {
  if (!response) {
    console.info('no cache response');
    throw new Error();
  }
  // console.info('cache response', response);
  return response;
}

this.addEventListener('fetch', function (event) {
  var cacheType = CACHE_URLS[event.request.url];
  if (!cacheType) return;

  // console.info('fetch', event.request.url);
  if (cacheType === 1) {
    event.respondWith(
      caches
        .match(event.request)
        .then(checkCacheResponse)
        .catch(noCache(event))
        .catch(function noCacheFail () {
          console.error('no cache fail');
        })
    );
  } else if (navigator.onLine) {
    var bustRequest = new Request(BLOG_POSTS);
    fetch(bustRequest).then(fetchSuccess({
      request: bustRequest
    }));
  }
});

this.addEventListener('activate', function (event) {
  event.waitUntil(
    caches.keys().then(function (keyList) {
      return Promise.all(keyList.map(function (key) {
        if (key !== CURRENT_VERSION) {
          return caches.delete(key);
        }
      }));
{'fraction_non_alphanumeric': 0.15597511263677322, 'fraction_numerical': 0.006650933276121004, 'mean_word_length': 3.296774193548387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 5, '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': '1179029', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1581, 'n_words': 267}