110 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-07-13 15:56:14 +00:00
// pack:ignore
2017-07-13 22:11:25 +00:00
var CACHE = "v-1"
2017-07-13 18:45:16 +00:00
self.addEventListener("install", (evt: any) => {
2017-07-13 22:11:25 +00:00
evt.waitUntil(
(self as any).skipWaiting().then(() => {
return installCache()
})
)
2017-07-13 18:45:16 +00:00
})
self.addEventListener("activate", (evt: any) => {
2017-07-13 22:11:25 +00:00
evt.waitUntil(
(self as any).clients.claim()
)
2017-07-13 18:45:16 +00:00
})
self.addEventListener("fetch", async (evt: any) => {
let request = evt.request
2017-07-13 22:11:25 +00:00
let isAuth = request.url.includes("/auth/") || request.url.includes("/logout")
2017-07-13 18:45:16 +00:00
2017-07-13 22:11:25 +00:00
// Delete existing cache on authentication
if(isAuth) {
caches.delete(CACHE)
}
2017-07-13 18:45:16 +00:00
// Do not use cache in some cases
2017-07-13 22:11:25 +00:00
if(request.method !== "GET" || isAuth || request.url.includes("chrome-extension")) {
2017-07-13 18:45:16 +00:00
return evt.waitUntil(evt.respondWith(fetch(request)))
}
2017-07-13 22:11:25 +00:00
let servedCachedResponse = false
2017-07-13 18:45:16 +00:00
// Start fetching the request
let refresh = fetch(request).then(response => {
let clone = response.clone()
// Save the new version of the resource in the cache
caches.open(CACHE).then(cache => {
2017-07-13 22:11:25 +00:00
return cache.put(request, clone)
}).then(() => {
if(!servedCachedResponse) {
return
}
let contentType = clone.headers.get("Content-Type")
if(contentType && contentType.startsWith("text/html") && clone.headers.get("ETag") && request.headers.get("X-Reload") !== "true") {
reloadContent(clone)
}
2017-07-13 18:45:16 +00:00
})
return response
})
2017-07-13 22:11:25 +00:00
// Forced reload
if(request.headers.get("X-Reload") === "true") {
return evt.waitUntil(refresh)
}
2017-07-13 18:45:16 +00:00
// Try to serve cache first and fall back to network response
2017-07-13 22:11:25 +00:00
let networkOrCache = fromCache(request).then(response => {
servedCachedResponse = true
return response
}).catch(error => {
// console.log("Cache MISS:", request.url)
2017-07-13 18:45:16 +00:00
return refresh
})
return evt.waitUntil(evt.respondWith(networkOrCache))
})
function installCache() {
return caches.open(CACHE).then(cache => {
return cache.addAll([
"./",
"./scripts",
"https://fonts.gstatic.com/s/ubuntu/v10/2Q-AW1e_taO6pHwMXcXW5w.ttf"
])
})
}
function fromCache(request) {
return caches.open(CACHE).then(cache => {
return cache.match(request).then(matching => {
if(matching) {
2017-07-13 22:11:25 +00:00
// console.log("Cache HIT:", request.url)
2017-07-13 18:45:16 +00:00
return Promise.resolve(matching)
}
return Promise.reject("no-match")
})
})
}
2017-07-13 22:11:25 +00:00
function reloadContent(response) {
return (self as any).clients.matchAll().then(clients => {
clients.forEach(client => {
var message = {
type: 'content changed',
url: response.url,
eTag: response.headers.get('ETag')
}
client.postMessage(JSON.stringify(message))
})
2017-07-13 18:45:16 +00:00
})
}