Improved service worker

This commit is contained in:
2017-07-19 07:39:09 +02:00
parent d5e1ce4e3a
commit 13978b7e8c
3 changed files with 44 additions and 29 deletions

View File

@ -17,6 +17,7 @@ export class AnimeNotifier {
visibilityObserver: IntersectionObserver
pushManager: PushManager
mainPageLoaded: boolean
lastReloadContentPath: string
imageFound: MutationQueue
imageNotFound: MutationQueue
@ -176,12 +177,7 @@ export class AnimeNotifier {
console.log("register service worker")
navigator.serviceWorker.register("/service-worker").then(registration => {
registration.update()
// if("sync" in registration) {
// registration.sync.register("background sync")
// } else {
// console.log("background sync not supported")
// }
// registration.update()
})
navigator.serviceWorker.addEventListener("message", evt => {
@ -194,6 +190,13 @@ export class AnimeNotifier {
return
}
// A reloadContent call should never trigger another reload
if(this.app.currentPath === this.lastReloadContentPath) {
console.log("reload finished.")
this.lastReloadContentPath = ""
return
}
let message = {
type: "loaded",
url: ""
@ -207,7 +210,7 @@ export class AnimeNotifier {
message.url = window.location.href
}
console.log("Loaded", message.url)
console.log("checking for updates:", message.url)
navigator.serviceWorker.controller.postMessage(JSON.stringify(message))
}
@ -316,10 +319,13 @@ export class AnimeNotifier {
}
reloadContent() {
console.log("reload content", "/_" + this.app.currentPath)
let headers = new Headers()
headers.append("X-Reload", "true")
let path = this.app.currentPath
this.lastReloadContentPath = path
return fetch("/_" + path, {
credentials: "same-origin",
@ -338,9 +344,10 @@ export class AnimeNotifier {
}
reloadPage() {
console.log("reload page")
console.log("reload page", this.app.currentPath)
let path = this.app.currentPath
this.lastReloadContentPath = path
return fetch(path, {
credentials: "same-origin"