This commit is contained in:
Eduard Urbach 2017-12-04 11:19:21 +01:00
parent 30994db2fa
commit 26ab168dd7
2 changed files with 33 additions and 35 deletions

View File

@ -88,24 +88,9 @@ export class ServiceWorkerManager {
break break
case "reload page": // case "offline":
console.log("service worker instructed to reload page...disobeying in test mode") // this.arn.statusMessage.showError("You are viewing an offline version of the site now.")
// location.reload(true) // break
break
case "reload styles":
console.log("service worker instructed to reload styles")
const links = document.getElementsByTagName("link")
for(const x in links) {
const link = links[x]
if(link.getAttribute("rel") === "stylesheet") {
link.href = link.href
}
}
break
} }
} }
} }

View File

@ -136,9 +136,12 @@ class MyServiceWorker {
// Save the served E-Tag when onResponse is called // Save the served E-Tag when onResponse is called
let servedETag = undefined let servedETag = undefined
let onResponse = (response: Response) => { let onResponse = (response: Response | null) => {
if(response) {
servedETag = response.headers.get("ETag") servedETag = response.headers.get("ETag")
ETAGS.set(request.url, servedETag) ETAGS.set(request.url, servedETag)
}
return response return response
} }
@ -276,14 +279,19 @@ class MyServiceWorker {
// Serve network first. // Serve network first.
// Fall back to cache. // Fall back to cache.
async networkFirst(request: Request, network: Promise<Response>, onResponse: (r: Response) => Response): Promise<Response> { async networkFirst(request: Request, network: Promise<Response>, onResponse: (r: Response) => Response): Promise<Response> {
let response: Response let response: Response | null
try { try {
response = await network response = await network
console.log("Network HIT:", request.url) console.log("Network HIT:", request.url)
} catch(error) { } catch(error) {
response = await this.fromCache(request)
console.log("Network MISS:", request.url, error) console.log("Network MISS:", request.url, error)
try {
response = await this.fromCache(request)
} catch(error) {
console.error(error)
}
} }
return onResponse(response) return onResponse(response)
@ -292,14 +300,19 @@ class MyServiceWorker {
// Serve cache first. // Serve cache first.
// Fall back to network. // Fall back to network.
async cacheFirst(request: Request, network: Promise<Response>, onResponse: (r: Response) => Response): Promise<Response> { async cacheFirst(request: Request, network: Promise<Response>, onResponse: (r: Response) => Response): Promise<Response> {
let response: Response let response: Response | null
try { try {
response = await this.fromCache(request) response = await this.fromCache(request)
console.log("Cache HIT:", request.url) console.log("Cache HIT:", request.url)
} catch(error) { } catch(error) {
response = await network
console.log("Cache MISS:", request.url, error) console.log("Cache MISS:", request.url, error)
try {
response = await network
} catch(error) {
console.error(error)
}
} }
return onResponse(response) return onResponse(response)
@ -439,18 +452,18 @@ class MyClient {
}) })
} }
async reloadPage(url: string) { // async reloadPage(url: string) {
let networkFetch = serviceWorker.reloads.get(url.replace("/_/", "/")) // let networkFetch = serviceWorker.reloads.get(url.replace("/_/", "/"))
if(networkFetch) { // if(networkFetch) {
await networkFetch // await networkFetch
} // }
return this.postMessage({ // return this.postMessage({
type: "reload page", // type: "reload page",
url // url
}) // })
} // }
reloadStyles() { reloadStyles() {
return this.postMessage({ return this.postMessage({