Disabled offline mode

This commit is contained in:
Eduard Urbach 2018-04-18 01:53:47 +02:00
parent 44623bbc1c
commit afdcc45c12

View File

@ -123,101 +123,103 @@ class MyServiceWorker {
return return
} }
// Exclude certain URLs from being cached. return fetch(request)
for(let pattern of this.excludeCache.keys()) {
if(request.url.includes(pattern)) {
return
}
}
// If the request has cache set to "force-cache", 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 this.excludeCache.keys()) {
if(request.headers.get("X-Force-Cache") === "true") { // if(request.url.includes(pattern)) {
return evt.respondWith(this.cache.serve(request)) // return
} // }
// }
// -------------------------------------------------------------------------------- // // If the request has cache set to "force-cache", return a cache-only response.
// Cross-origin requests. // // This is used in reloads to avoid generating a 2nd request after a cache refresh.
// -------------------------------------------------------------------------------- // if(request.headers.get("X-Force-Cache") === "true") {
// return evt.respondWith(this.cache.serve(request))
// }
// These hosts don't support CORS. Always load via network. // // --------------------------------------------------------------------------------
if(request.url.startsWith("https://img.youtube.com/")) { // // Cross-origin requests.
return // // --------------------------------------------------------------------------------
}
// Use CORS for cross-origin requests. // // These hosts don't support CORS. Always load via network.
if(!request.url.startsWith("https://notify.moe/") && !request.url.startsWith("https://beta.notify.moe/")) { // if(request.url.startsWith("https://img.youtube.com/")) {
request = new Request(request.url, { // return
credentials: "omit", // }
mode: "cors"
})
} else {
// let relativePath = trimPrefix(request.url, "https://notify.moe")
// relativePath = trimPrefix(relativePath, "https://beta.notify.moe")
// console.log(relativePath)
}
// -------------------------------------------------------------------------------- // // Use CORS for cross-origin requests.
// Network refresh. // if(!request.url.startsWith("https://notify.moe/") && !request.url.startsWith("https://beta.notify.moe/")) {
// -------------------------------------------------------------------------------- // request = new Request(request.url, {
// credentials: "omit",
// mode: "cors"
// })
// } else {
// // let relativePath = trimPrefix(request.url, "https://notify.moe")
// // relativePath = trimPrefix(relativePath, "https://beta.notify.moe")
// // console.log(relativePath)
// }
// Save response in cache. // // --------------------------------------------------------------------------------
let saveResponseInCache = (response: Response) => { // // Network refresh.
let contentType = response.headers.get("Content-Type") // // --------------------------------------------------------------------------------
// Don't cache anything other than text, styles, scripts, fonts and images. // // Save response in cache.
if(!contentType.includes("text/") && !contentType.includes("application/javascript") && !contentType.includes("image/") && !contentType.includes("font/")) { // let saveResponseInCache = (response: Response) => {
return response // let contentType = response.headers.get("Content-Type")
}
// Save response in cache. // // Don't cache anything other than text, styles, scripts, fonts and images.
if(response.ok) { // if(!contentType.includes("text/") && !contentType.includes("application/javascript") && !contentType.includes("image/") && !contentType.includes("font/")) {
let clone = response.clone() // return response
this.cache.store(request, clone) // }
}
return response // // Save response in cache.
} // if(response.ok) {
// let clone = response.clone()
// this.cache.store(request, clone)
// }
let onResponse = (response: Response | null) => { // return response
return response // }
}
// Refresh resource via a network request. // let onResponse = (response: Response | null) => {
let refresh = fetch(request).then(saveResponseInCache) // return response
// }
// -------------------------------------------------------------------------------- // // Refresh resource via a network request.
// Final response. // let refresh = fetch(request).then(saveResponseInCache)
// --------------------------------------------------------------------------------
// Clear cache on authentication and fetch it normally. // // --------------------------------------------------------------------------------
if(request.url.includes("/auth/") || request.url.includes("/logout")) { // // Final response.
return evt.respondWith(this.cache.clear().then(() => refresh)) // // --------------------------------------------------------------------------------
}
// If the request has cache set to "no-cache", // // Clear cache on authentication and fetch it normally.
// return the network-only response even if it fails. // if(request.url.includes("/auth/") || request.url.includes("/logout")) {
if(request.headers.get("X-No-Cache") === "true") { // return evt.respondWith(this.cache.clear().then(() => refresh))
return evt.respondWith(refresh) // }
}
// Styles and scripts will be served via network first and fallback to cache. // // If the request has cache set to "no-cache",
if(request.url.endsWith("/styles") || request.url.endsWith("/scripts")) { // // return the network-only response even if it fails.
evt.respondWith(this.networkFirst(request, refresh, onResponse)) // if(request.headers.get("X-No-Cache") === "true") {
return refresh // return evt.respondWith(refresh)
} // }
// -------------------------------------------------------------------------------- // // Styles and scripts will be served via network first and fallback to cache.
// Default behavior for most requests. // if(request.url.endsWith("/styles") || request.url.endsWith("/scripts")) {
// -------------------------------------------------------------------------------- // evt.respondWith(this.networkFirst(request, refresh, onResponse))
// return refresh
// }
// // Respond via cache first. // // --------------------------------------------------------------------------------
// evt.respondWith(this.cacheFirst(request, refresh, onResponse)) // // Default behavior for most requests.
// // --------------------------------------------------------------------------------
// // // Respond via cache first.
// // evt.respondWith(this.cacheFirst(request, refresh, onResponse))
// // return refresh
// // Serve via network first and fallback to cache.
// evt.respondWith(this.networkFirst(request, refresh, onResponse))
// return refresh // return refresh
// Serve via network first and fallback to cache.
evt.respondWith(this.networkFirst(request, refresh, onResponse))
return refresh
} }
// 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).