Disabled service worker caching

This commit is contained in:
Eduard Urbach 2018-02-20 09:10:44 +01:00
parent 51bcee6aed
commit f47fb93ddd

View File

@ -61,7 +61,7 @@ class MyServiceWorker {
reloads: Map<string, Promise<Response>> reloads: Map<string, Promise<Response>>
constructor() { constructor() {
this.cache = new MyCache("v-5") this.cache = new MyCache("v-6")
this.reloads = new Map<string, Promise<Response>>() this.reloads = new Map<string, Promise<Response>>()
self.addEventListener("install", (evt: InstallEvent) => evt.waitUntil(this.onInstall(evt))) self.addEventListener("install", (evt: InstallEvent) => evt.waitUntil(this.onInstall(evt)))
@ -106,80 +106,82 @@ class MyServiceWorker {
// onRequest intercepts all browser requests // onRequest intercepts all browser requests
onRequest(evt: FetchEvent) { onRequest(evt: FetchEvent) {
let request = evt.request as Request return evt.respondWith(this.fromNetwork(evt.request))
// If it's not a GET request, fetch it normally // let request = evt.request as Request
if(request.method !== "GET") {
return evt.respondWith(this.fromNetwork(request))
}
// Clear cache on authentication and fetch it normally // // If it's not a GET request, fetch it normally
if(request.url.includes("/auth/") || request.url.includes("/logout")) { // if(request.method !== "GET") {
return evt.respondWith(caches.delete(this.cache.version).then(() => fetch(request))) // return evt.respondWith(this.fromNetwork(request))
} // }
// Exclude certain URLs from being cached // // Clear cache on authentication and fetch it normally
for(let pattern of EXCLUDECACHE.keys()) { // if(request.url.includes("/auth/") || request.url.includes("/logout")) {
if(request.url.includes(pattern)) { // return evt.respondWith(caches.delete(this.cache.version).then(() => fetch(request)))
return evt.respondWith(this.fromNetwork(request)) // }
}
}
// If the request included the header "X-CacheOnly", return a cache-only response. // // Exclude certain URLs from being cached
// This is used in reloads to avoid generating a 2nd request after a cache refresh. // for(let pattern of EXCLUDECACHE.keys()) {
if(request.headers.get("X-CacheOnly") === "true") { // if(request.url.includes(pattern)) {
return evt.respondWith(this.fromCache(request)) // return evt.respondWith(this.fromNetwork(request))
} // }
// }
// Save the served E-Tag when onResponse is called // // If the request included the header "X-CacheOnly", return a cache-only response.
let servedETag = undefined // // This is used in reloads to avoid generating a 2nd request after a cache refresh.
// if(request.headers.get("X-CacheOnly") === "true") {
// return evt.respondWith(this.fromCache(request))
// }
let onResponse = (response: Response | null) => { // // Save the served E-Tag when onResponse is called
if(response) { // let servedETag = undefined
servedETag = response.headers.get("ETag")
ETAGS.set(request.url, servedETag)
}
return response // let onResponse = (response: Response | null) => {
} // if(response) {
// servedETag = response.headers.get("ETag")
// ETAGS.set(request.url, servedETag)
// }
let saveResponseInCache = response => { // return response
let clone = response.clone() // }
// Save the new version of the resource in the cache // let saveResponseInCache = response => {
let cacheRefresh = this.cache.store(request, clone).catch(err => { // let clone = response.clone()
console.error(err)
// TODO: Tell client that the quota is exceeded (disk full).
})
CACHEREFRESH.set(request.url, cacheRefresh) // // Save the new version of the resource in the cache
return response // let cacheRefresh = this.cache.store(request, clone).catch(err => {
} // console.error(err)
// // TODO: Tell client that the quota is exceeded (disk full).
// })
// Start fetching the request // CACHEREFRESH.set(request.url, cacheRefresh)
let network = // return response
fetch(request) // }
.then(saveResponseInCache)
.catch(error => {
console.log("Fetch error:", error)
throw error
})
// Save in map // // Start fetching the request
this.reloads.set(request.url, network) // let network =
// fetch(request)
// .then(saveResponseInCache)
// .catch(error => {
// console.log("Fetch error:", error)
// throw error
// })
if(request.headers.get("X-Reload") === "true") { // // Save in map
return evt.respondWith(network) // this.reloads.set(request.url, network)
}
// Scripts and styles are server pushed on the initial response // if(request.headers.get("X-Reload") === "true") {
// so we can use a network-first response without an additional round-trip. // return evt.respondWith(network)
// This causes the browser to always load the most recent scripts and styles. // }
if(request.url.endsWith("/styles") || request.url.endsWith("/scripts")) {
return evt.respondWith(this.networkFirst(request, network, onResponse))
}
return evt.respondWith(this.cacheFirst(request, network, onResponse)) // // Scripts and styles are server pushed on the initial response
// // so we can use a network-first response without an additional round-trip.
// // This causes the browser to always load the most recent scripts and styles.
// if(request.url.endsWith("/styles") || request.url.endsWith("/scripts")) {
// return evt.respondWith(this.networkFirst(request, network, onResponse))
// }
// return evt.respondWith(this.cacheFirst(request, network, onResponse))
} }
// onMessage is called when the service worker receives a message from a client (browser tab). // onMessage is called when the service worker receives a message from a client (browser tab).