Improved service worker
This commit is contained in:
parent
3ee21850c8
commit
d1603fad40
@ -16,6 +16,7 @@ export class AnimeNotifier {
|
|||||||
statusMessage: StatusMessage
|
statusMessage: StatusMessage
|
||||||
visibilityObserver: IntersectionObserver
|
visibilityObserver: IntersectionObserver
|
||||||
pushManager: PushManager
|
pushManager: PushManager
|
||||||
|
lastRequestURL: string
|
||||||
|
|
||||||
imageFound: MutationQueue
|
imageFound: MutationQueue
|
||||||
imageNotFound: MutationQueue
|
imageNotFound: MutationQueue
|
||||||
@ -176,12 +177,18 @@ export class AnimeNotifier {
|
|||||||
|
|
||||||
navigator.serviceWorker.register("/service-worker").then(registration => {
|
navigator.serviceWorker.register("/service-worker").then(registration => {
|
||||||
registration.update()
|
registration.update()
|
||||||
|
// if("sync" in registration) {
|
||||||
|
// registration.sync.register("background sync")
|
||||||
|
// } else {
|
||||||
|
// console.log("background sync not supported")
|
||||||
|
// }
|
||||||
})
|
})
|
||||||
|
|
||||||
navigator.serviceWorker.addEventListener("message", evt => {
|
navigator.serviceWorker.addEventListener("message", evt => {
|
||||||
this.onServiceWorkerMessage(evt)
|
this.onServiceWorkerMessage(evt)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// This will send a message to the service worker that the DOM has been loaded
|
||||||
let sendContentLoadedEvent = () => {
|
let sendContentLoadedEvent = () => {
|
||||||
if(!navigator.serviceWorker.controller) {
|
if(!navigator.serviceWorker.controller) {
|
||||||
return
|
return
|
||||||
@ -192,14 +199,14 @@ export class AnimeNotifier {
|
|||||||
url: ""
|
url: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this.app.lastRequest) {
|
if(this.lastRequestURL) {
|
||||||
|
message.url = this.lastRequestURL
|
||||||
|
} else if(this.app.lastRequest) {
|
||||||
message.url = this.app.lastRequest.responseURL
|
message.url = this.app.lastRequest.responseURL
|
||||||
} else {
|
} else {
|
||||||
message.url = window.location.href
|
message.url = window.location.href
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("send loaded event to service worker")
|
|
||||||
|
|
||||||
navigator.serviceWorker.controller.postMessage(JSON.stringify(message))
|
navigator.serviceWorker.controller.postMessage(JSON.stringify(message))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,8 +318,9 @@ export class AnimeNotifier {
|
|||||||
headers.append("X-Reload", "true")
|
headers.append("X-Reload", "true")
|
||||||
|
|
||||||
let path = this.app.currentPath
|
let path = this.app.currentPath
|
||||||
|
this.lastRequestURL = location.origin + "/_" + path
|
||||||
|
|
||||||
return fetch("/_" + path, {
|
return fetch(this.lastRequestURL, {
|
||||||
credentials: "same-origin",
|
credentials: "same-origin",
|
||||||
headers
|
headers
|
||||||
})
|
})
|
||||||
@ -331,14 +339,10 @@ export class AnimeNotifier {
|
|||||||
reloadPage() {
|
reloadPage() {
|
||||||
console.log("reload page")
|
console.log("reload page")
|
||||||
|
|
||||||
let headers = new Headers()
|
|
||||||
headers.append("X-Reload", "true")
|
|
||||||
|
|
||||||
let path = this.app.currentPath
|
let path = this.app.currentPath
|
||||||
|
|
||||||
return fetch(path, {
|
return fetch(path, {
|
||||||
credentials: "same-origin",
|
credentials: "same-origin"
|
||||||
headers
|
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if(this.app.currentPath !== path) {
|
if(this.app.currentPath !== path) {
|
||||||
@ -509,7 +513,10 @@ export class AnimeNotifier {
|
|||||||
return Promise.reject(null)
|
return Promise.reject(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
let request = fetch("/_" + url, {
|
let path = "/_" + url
|
||||||
|
this.lastRequestURL = location.origin + "/_" + path
|
||||||
|
|
||||||
|
let request = fetch(path, {
|
||||||
credentials: "same-origin"
|
credentials: "same-origin"
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
@ -48,6 +48,24 @@ self.addEventListener("message", (evt: any) => {
|
|||||||
let refresh = RELOADS.get(url)
|
let refresh = RELOADS.get(url)
|
||||||
let servedETag = ETAGS.get(url)
|
let servedETag = ETAGS.get(url)
|
||||||
|
|
||||||
|
// If the user requests a sub-page we should prefetch the full page, too.
|
||||||
|
if(url.includes("/_/")) {
|
||||||
|
let fullPage = new Request(url.replace("/_/", "/"))
|
||||||
|
|
||||||
|
fetch(fullPage, {
|
||||||
|
credentials: "same-origin"
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
// Save the new version of the resource in the cache
|
||||||
|
let cacheRefresh = caches.open(CACHE).then(cache => {
|
||||||
|
return cache.put(fullPage, response)
|
||||||
|
})
|
||||||
|
|
||||||
|
CACHEREFRESH.set(fullPage.url, cacheRefresh)
|
||||||
|
return cacheRefresh
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if(!refresh || !servedETag) {
|
if(!refresh || !servedETag) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -73,11 +91,6 @@ self.addEventListener("message", (evt: any) => {
|
|||||||
url
|
url
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a subpage has refreshed, refresh the main page cache, too.
|
|
||||||
// if(url.includes("/_/")) {
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
let cacheRefresh = CACHEREFRESH.get(url)
|
let cacheRefresh = CACHEREFRESH.get(url)
|
||||||
|
|
||||||
if(!cacheRefresh) {
|
if(!cacheRefresh) {
|
||||||
@ -91,6 +104,21 @@ self.addEventListener("message", (evt: any) => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// self.addEventListener("sync", (evt: any) => {
|
||||||
|
// console.log(evt.tag)
|
||||||
|
|
||||||
|
// let fetches = new Array<Promise<void>>()
|
||||||
|
|
||||||
|
// for(let url of BACKGROUNDFETCHES.keys()) {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// console.log("background fetching:", BACKGROUNDFETCHES.keys())
|
||||||
|
// BACKGROUNDFETCHES.clear()
|
||||||
|
|
||||||
|
// evt.waitUntil(Promise.all(fetches))
|
||||||
|
// })
|
||||||
|
|
||||||
self.addEventListener("fetch", async (evt: FetchEvent) => {
|
self.addEventListener("fetch", async (evt: FetchEvent) => {
|
||||||
let request = evt.request as Request
|
let request = evt.request as Request
|
||||||
let isAuth = request.url.includes("/auth/") || request.url.includes("/logout")
|
let isAuth = request.url.includes("/auth/") || request.url.includes("/logout")
|
||||||
|
Loading…
Reference in New Issue
Block a user