Skip to content

Instantly share code, notes, and snippets.

@jlengrand
Last active September 21, 2019 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlengrand/9645354edfc0398a60acfc69476f145d to your computer and use it in GitHub Desktop.
Save jlengrand/9645354edfc0398a60acfc69476f145d to your computer and use it in GitHub Desktop.
"use strict";
var precacheConfig = [
["/index.html", "6633bcbb6d392a7d68be37924b341845"],
["/static/js/main.f95e1c59.chunk.js", "559581975d6026fccabb204f217e44db"],
["/static/js/runtime~main.1b922744.js", "13a6cba6703084ad6f60a3db941db615"],
["/static/js/vendors~main.3ca81432.chunk.js", "86fba34edc08a61c670b6c7527e6b813"]
],
cacheName = "sw-precache-v3-sw-precache-webpack-plugin-" + (self.registration ? self.registration.scope : ""),
ignoreUrlParametersMatching = [/^utm_/],
addDirectoryIndex = function(e, n) {
var t = new URL(e);
return "/" === t.pathname.slice(-1) && (t.pathname += n), t.toString()
},
cleanResponse = function(e) {
return e.redirected ? ("body" in e ? Promise.resolve(e.body) : e.blob()).then(function(n) {
return new Response(n, {
headers: e.headers,
status: e.status,
statusText: e.statusText
})
}) : Promise.resolve(e)
},
createCacheKey = function(e, n, t, r) {
var a = new URL(e);
return r && a.pathname.match(r) || (a.search += (a.search ? "&" : "") + encodeURIComponent(n) + "=" + encodeURIComponent(t)), a.toString()
},
isPathWhitelisted = function(e, n) {
if (0 === e.length) return !0;
var t = new URL(n).pathname;
return e.some(function(e) {
return t.match(e)
})
},
stripIgnoredUrlParameters = function(e, n) {
var t = new URL(e);
return t.hash = "", t.search = t.search.slice(1).split("&").map(function(e) {
return e.split("=")
}).filter(function(e) {
return n.every(function(n) {
return !n.test(e[0])
})
}).map(function(e) {
return e.join("=")
}).join("&"), t.toString()
},
hashParamName = "_sw-precache",
urlsToCacheKeys = new Map(precacheConfig.map(function(e) {
var n = e[0],
t = e[1],
r = new URL(n, self.location),
a = createCacheKey(r, hashParamName, t, /\.\w{8}\./);
return [r.toString(), a]
}));
function setOfCachedUrls(e) {
return e.keys().then(function(e) {
return e.map(function(e) {
return e.url
})
}).then(function(e) {
return new Set(e)
})
}
self.addEventListener("install", function(e) {
e.waitUntil(caches.open(cacheName).then(function(e) {
return setOfCachedUrls(e).then(function(n) {
return Promise.all(Array.from(urlsToCacheKeys.values()).map(function(t) {
if (!n.has(t)) {
var r = new Request(t, {
credentials: "same-origin"
});
return fetch(r).then(function(n) {
if (!n.ok) throw new Error("Request for " + t + " returned a response with status " + n.status);
return cleanResponse(n).then(function(n) {
return e.put(t, n)
})
})
}
}))
})
}).then(function() {
return self.skipWaiting()
}))
}), self.addEventListener("activate", function(e) {
var n = new Set(urlsToCacheKeys.values());
e.waitUntil(caches.open(cacheName).then(function(e) {
return e.keys().then(function(t) {
return Promise.all(t.map(function(t) {
if (!n.has(t.url)) return e.delete(t)
}))
})
}).then(function() {
return self.clients.claim()
}))
}), self.addEventListener("fetch", function(e) {
if ("GET" === e.request.method) {
var n, t = stripIgnoredUrlParameters(e.request.url, ignoreUrlParametersMatching);
(n = urlsToCacheKeys.has(t)) || (t = addDirectoryIndex(t, "index.html"), n = urlsToCacheKeys.has(t));
0, n && e.respondWith(caches.open(cacheName).then(function(e) {
return e.match(urlsToCacheKeys.get(t)).then(function(e) {
if (e) return e;
throw Error("The cached response that was expected is missing.")
})
}).catch(function(n) {
return console.warn('Couldn\'t serve response for "%s" from cache: %O', e.request.url, n), fetch(e.request)
}))
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment