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);
        }
      }));
    })
  );
});

@@ -429,24 +429,20 @@
 var 
-CACHE_URLS = %7B%0A 
+BLOG_POSTS =
  'ht
@@ -497,12 +497,29 @@
 sts'
-: 1,
+;%0A%0Avar CACHE_URLS = %7B
 %0A  '
@@ -595,10 +595,38 @@
 : 2%0A
-
 %7D;
+%0ACACHE_URLS%5BBLOG_POSTS%5D = 1;
 %0A%0Afu
@@ -1938,53 +1938,18 @@
 est(
-'https://api.cosmicjs.com/v1/blog-cb/objects'
+BLOG_POSTS
 );%0A 

Fix up URL