Skip to content

Instantly share code, notes, and snippets.

@charlesBochet
Created June 26, 2017 21:59
Show Gist options
  • Save charlesBochet/69115ea4309b067e5acbbdc419431d81 to your computer and use it in GitHub Desktop.
Save charlesBochet/69115ea4309b067e5acbbdc419431d81 to your computer and use it in GitHub Desktop.
"use strict";
function setOfCachedUrls(e) {
return e.keys().then(function (e) {
return e.map(function (e) {
return e.url
})
}).then(function (e) {
return new Set(e)
})
}
var precacheConfig = [["index.html", "218187414cc275eaa7bb37f098e0fc92"], ["static/css/app.d1a62a5e00430713ca6ccd67cb5fd580.css", "d1a62a5e00430713ca6ccd67cb5fd580"], ["static/js/app.04ef0c51d385926ea560.js", "13cfbbcfd7077380247bbbb6d9731463"], ["static/js/manifest.2d92f59992387f01763b.js", "00451931f39fbee842952a5d349f22a6"], ["static/js/vendor.0140506021c3a9c89dd2.js", "27f61c490b1b8af1d621b16afdd174bb"]], cacheName = "sw-precache-v3-my-vue-app-" + (self.registration ? self.registration.scope : ""), ignoreUrlParametersMatching = [/^utm_/], addDirectoryIndex = function (e, t) {
var n = new URL(e);
return "/" === n.pathname.slice(-1) && (n.pathname += t), n.toString()
}, cleanResponse = function (e) {
return e.redirected ? ("body" in e ? Promise.resolve(e.body) : e.blob()).then(function (t) {
return new Response(t, {headers: e.headers, status: e.status, statusText: e.statusText})
}) : Promise.resolve(e)
}, createCacheKey = function (e, t, n, r) {
var a = new URL(e);
return r && a.pathname.match(r) || (a.search += (a.search ? "&" : "") + encodeURIComponent(t) + "=" + encodeURIComponent(n)), a.toString()
}, isPathWhitelisted = function (e, t) {
if (0 === e.length)return !0;
var n = new URL(t).pathname;
return e.some(function (e) {
return n.match(e)
})
}, stripIgnoredUrlParameters = function (e, t) {
var n = new URL(e);
return n.hash = "", n.search = n.search.slice(1).split("&").map(function (e) {
return e.split("=")
}).filter(function (e) {
return t.every(function (t) {
return !t.test(e[0])
})
}).map(function (e) {
return e.join("=")
}).join("&"), n.toString()
}, hashParamName = "_sw-precache", urlsToCacheKeys = new Map(precacheConfig.map(function (e) {
var t = e[0], n = e[1], r = new URL(t, self.location), a = createCacheKey(r, hashParamName, n, !1);
return [r.toString(), a]
}));
self.addEventListener("install", function (e) {
e.waitUntil(caches.open(cacheName).then(function (e) {
return setOfCachedUrls(e).then(function (t) {
return Promise.all(Array.from(urlsToCacheKeys.values()).map(function (n) {
if (!t.has(n)) {
var r = new Request(n, {credentials: "same-origin"});
return fetch(r).then(function (t) {
if (!t.ok)throw new Error("Request for " + n + " returned a response with status " + t.status);
return cleanResponse(t).then(function (t) {
return e.put(n, t)
})
})
}
}))
})
}).then(function () {
return self.skipWaiting()
}))
}), self.addEventListener("activate", function (e) {
var t = new Set(urlsToCacheKeys.values());
e.waitUntil(caches.open(cacheName).then(function (e) {
return e.keys().then(function (n) {
return Promise.all(n.map(function (n) {
if (!t.has(n.url))return e.delete(n)
}))
})
}).then(function () {
return self.clients.claim()
}))
}), self.addEventListener("fetch", function (e) {
if ("GET" === e.request.method) {
var t, n = stripIgnoredUrlParameters(e.request.url, ignoreUrlParametersMatching);
t = urlsToCacheKeys.has(n);
t || (n = addDirectoryIndex(n, "index.html"), t = urlsToCacheKeys.has(n));
t && e.respondWith(caches.open(cacheName).then(function (e) {
return e.match(urlsToCacheKeys.get(n)).then(function (e) {
if (e)return e;
throw Error("The cached response that was expected is missing.")
})
}).catch(function (t) {
return console.warn('Couldn\'t serve response for "%s" from cache: %O', e.request.url, t), fetch(e.request)
}))
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment