134 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-07-13 15:56:14 +00:00
// pack:ignore
2017-07-14 02:58:21 +00:00
const CACHE = "v-1"
const RELOADS = new Map<string, Promise<Response>>()
const ETAGS = new Map<string, string>()
2017-07-13 18:45:16 +00:00
self.addEventListener("install", (evt: any) => {
2017-07-14 02:58:21 +00:00
console.log("Service worker install")
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-14 02:58:21 +00:00
console.log("Service worker activate")
2017-07-13 22:11:25 +00:00
evt.waitUntil(
(self as any).clients.claim()
)
2017-07-13 18:45:16 +00:00
})
2017-07-14 02:58:21 +00:00
// controlling service worker
self.addEventListener("message", (evt: any) => {
let message = JSON.parse(evt.data)
let url = message.url
let refresh = RELOADS.get(url)
let servedETag = ETAGS.get(url)
if(!refresh || !servedETag) {
return
}
evt.waitUntil(
refresh.then((response: Response) => {
// If the fresh copy was used to serve the request instead of the cache,
// we don't need to tell the client to do a refresh.
if(response.bodyUsed) {
return
}
let eTag = response.headers.get("ETag")
if(eTag === servedETag) {
return
}
ETAGS.set(url, eTag)
let message = {
type: "new content",
url
}
return evt.source.postMessage(JSON.stringify(message))
})
)
})
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-14 02:58:21 +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
2017-07-14 02:58:21 +00:00
let servedETag = undefined
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)
2017-07-13 18:45:16 +00:00
})
return response
})
2017-07-14 02:58:21 +00:00
// Save in map
RELOADS.set(request.url, refresh)
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 => {
2017-07-14 02:58:21 +00:00
servedETag = response.headers.get("ETag")
ETAGS.set(request.url, servedETag)
2017-07-13 22:11:25 +00:00
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")
})
})
}