litejs
Version:
Single-page application framework
65 lines (51 loc) • 1.19 kB
JavaScript
var VERSION = "2019"
, FILES = [
"index.html"
]
, cacheList = [
VERSION
]
, isFileRe = /\/[-\.\w]+\.\w+$/
function log() {
//console.log.apply(console.log, arguments)
}
addEventListener("install", function(event) {
log("install", VERSION)
event.waitUntil(
caches.open(VERSION)
.then(cache => cache.addAll(FILES))
)
})
addEventListener("activate", function(event) {
log("activate", VERSION)
event.waitUntil(clients.claim())
caches.keys()
.then(keys => Promise.all(keys.map(key => {
if (cacheList.indexOf(key) < 0) {
log("caches.delete", key)
return caches.delete(key)
}
})))
})
addEventListener("fetch", function(event) {
var url = event.request.url.slice(registration.scope.length).split("?")[0] || "index.html"
if (
event.request.method === "GET" &&
isFileRe.test(url)
) event.respondWith(
caches.open(VERSION)
.then(function(cache) {
return cache.match(url, { ignoreSearch: true })
.then(function(response) {
if (!response) log("MISS", url)
return response || fetch(event.request)
})
})
)
})
addEventListener("message", function(event) {
var op = event.data.op || event.data
if (op === "reload") {
skipWaiting()
}
})