Upgrade service worker cache
This commit is contained in:
parent
d07ae4ebf6
commit
3e9c2dc746
@ -41,9 +41,9 @@ class MyServiceWorker {
|
|||||||
currentCSP: string
|
currentCSP: string
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.cache = new MyCache("v-3")
|
this.cache = new MyCache("v-4")
|
||||||
this.currentCSP = ""
|
this.currentCSP = ""
|
||||||
|
|
||||||
self.addEventListener("install", (evt: InstallEvent) => evt.waitUntil(this.onInstall(evt)))
|
self.addEventListener("install", (evt: InstallEvent) => evt.waitUntil(this.onInstall(evt)))
|
||||||
self.addEventListener("activate", (evt: any) => evt.waitUntil(this.onActivate(evt)))
|
self.addEventListener("activate", (evt: any) => evt.waitUntil(this.onActivate(evt)))
|
||||||
self.addEventListener("fetch", (evt: FetchEvent) => evt.waitUntil(this.onRequest(evt)))
|
self.addEventListener("fetch", (evt: FetchEvent) => evt.waitUntil(this.onRequest(evt)))
|
||||||
@ -55,7 +55,7 @@ class MyServiceWorker {
|
|||||||
|
|
||||||
onInstall(evt: InstallEvent) {
|
onInstall(evt: InstallEvent) {
|
||||||
console.log("service worker install")
|
console.log("service worker install")
|
||||||
|
|
||||||
return (self as any).skipWaiting().then(() => {
|
return (self as any).skipWaiting().then(() => {
|
||||||
return this.installCache()
|
return this.installCache()
|
||||||
})
|
})
|
||||||
@ -63,10 +63,10 @@ class MyServiceWorker {
|
|||||||
|
|
||||||
onActivate(evt: any) {
|
onActivate(evt: any) {
|
||||||
console.log("service worker activate")
|
console.log("service worker activate")
|
||||||
|
|
||||||
// Only keep current version of the cache and delete old caches
|
// Only keep current version of the cache and delete old caches
|
||||||
let cacheWhitelist = [this.cache.version]
|
let cacheWhitelist = [this.cache.version]
|
||||||
|
|
||||||
let deleteOldCache = caches.keys().then(keyList => {
|
let deleteOldCache = caches.keys().then(keyList => {
|
||||||
return Promise.all(keyList.map(key => {
|
return Promise.all(keyList.map(key => {
|
||||||
if(cacheWhitelist.indexOf(key) === -1) {
|
if(cacheWhitelist.indexOf(key) === -1) {
|
||||||
@ -74,10 +74,10 @@ class MyServiceWorker {
|
|||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Immediate claim helps us gain control over a new client immediately
|
// Immediate claim helps us gain control over a new client immediately
|
||||||
let immediateClaim = (self as any).clients.claim()
|
let immediateClaim = (self as any).clients.claim()
|
||||||
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
deleteOldCache,
|
deleteOldCache,
|
||||||
immediateClaim
|
immediateClaim
|
||||||
@ -103,28 +103,28 @@ class MyServiceWorker {
|
|||||||
return evt.respondWith(fetch(request))
|
return evt.respondWith(fetch(request))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the request included the header "X-CacheOnly", return a cache-only response.
|
// If the request included the header "X-CacheOnly", return a cache-only response.
|
||||||
// This is used in reloads to avoid generating a 2nd request after a cache refresh.
|
// This is used in reloads to avoid generating a 2nd request after a cache refresh.
|
||||||
if(request.headers.get("X-CacheOnly") === "true") {
|
if(request.headers.get("X-CacheOnly") === "true") {
|
||||||
return evt.respondWith(this.fromCache(request))
|
return evt.respondWith(this.fromCache(request))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start fetching the request
|
// Start fetching the request
|
||||||
let refresh = fetch(request).then(response => {
|
let refresh = fetch(request).then(response => {
|
||||||
let clone = response.clone()
|
let clone = response.clone()
|
||||||
|
|
||||||
// Save the new version of the resource in the cache
|
// Save the new version of the resource in the cache
|
||||||
let cacheRefresh = this.cache.store(request, clone)
|
let cacheRefresh = this.cache.store(request, clone)
|
||||||
|
|
||||||
CACHEREFRESH.set(request.url, cacheRefresh)
|
CACHEREFRESH.set(request.url, cacheRefresh)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
})
|
})
|
||||||
|
|
||||||
// Save in map
|
// Save in map
|
||||||
RELOADS.set(request.url, refresh)
|
RELOADS.set(request.url, refresh)
|
||||||
|
|
||||||
// Forced reload
|
// Forced reload
|
||||||
let servedETag = undefined
|
let servedETag = undefined
|
||||||
|
|
||||||
@ -137,13 +137,13 @@ class MyServiceWorker {
|
|||||||
if(request.headers.get("X-Reload") === "true") {
|
if(request.headers.get("X-Reload") === "true") {
|
||||||
return evt.respondWith(refresh.then(onResponse))
|
return evt.respondWith(refresh.then(onResponse))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to serve cache first and fall back to network response
|
// Try to serve cache first and fall back to network response
|
||||||
let networkOrCache = this.fromCache(request).then(onResponse).catch(error => {
|
let networkOrCache = this.fromCache(request).then(onResponse).catch(error => {
|
||||||
// console.log("Cache MISS:", request.url)
|
// console.log("Cache MISS:", request.url)
|
||||||
return refresh
|
return refresh
|
||||||
})
|
})
|
||||||
|
|
||||||
return evt.respondWith(networkOrCache)
|
return evt.respondWith(networkOrCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,12 +162,12 @@ class MyServiceWorker {
|
|||||||
onDOMContentLoaded(evt: any, url: string) {
|
onDOMContentLoaded(evt: any, url: string) {
|
||||||
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 the user requests a sub-page we should prefetch the full page, too.
|
||||||
if(url.includes("/_/")) {
|
if(url.includes("/_/")) {
|
||||||
this.prefetchFullPage(url)
|
this.prefetchFullPage(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!refresh || !servedETag) {
|
if(!refresh || !servedETag) {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
@ -178,7 +178,7 @@ class MyServiceWorker {
|
|||||||
if(response.bodyUsed) {
|
if(response.bodyUsed) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the ETag of the cached response we sent to the client earlier.
|
// Get the ETag of the cached response we sent to the client earlier.
|
||||||
let eTag = response.headers.get("ETag")
|
let eTag = response.headers.get("ETag")
|
||||||
|
|
||||||
@ -202,7 +202,7 @@ class MyServiceWorker {
|
|||||||
if(eTag !== servedETag) {
|
if(eTag !== servedETag) {
|
||||||
return this.forceClientReloadContent(url, evt.source)
|
return this.forceClientReloadContent(url, evt.source)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do nothing
|
// Do nothing
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
})
|
})
|
||||||
@ -210,7 +210,7 @@ class MyServiceWorker {
|
|||||||
|
|
||||||
prefetchFullPage(url: string) {
|
prefetchFullPage(url: string) {
|
||||||
let fullPage = new Request(url.replace("/_/", "/"))
|
let fullPage = new Request(url.replace("/_/", "/"))
|
||||||
|
|
||||||
let fullPageRefresh = fetch(fullPage, {
|
let fullPageRefresh = fetch(fullPage, {
|
||||||
credentials: "same-origin"
|
credentials: "same-origin"
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
@ -229,7 +229,7 @@ class MyServiceWorker {
|
|||||||
|
|
||||||
onPush(evt: PushEvent) {
|
onPush(evt: PushEvent) {
|
||||||
var payload = evt.data ? evt.data.json() : {}
|
var payload = evt.data ? evt.data.json() : {}
|
||||||
|
|
||||||
return (self as any).registration.showNotification(payload.title, {
|
return (self as any).registration.showNotification(payload.title, {
|
||||||
body: payload.message,
|
body: payload.message,
|
||||||
icon: payload.icon,
|
icon: payload.icon,
|
||||||
@ -243,15 +243,15 @@ class MyServiceWorker {
|
|||||||
return (self as any).registration.pushManager.subscribe(evt.oldSubscription.options)
|
return (self as any).registration.pushManager.subscribe(evt.oldSubscription.options)
|
||||||
.then(async subscription => {
|
.then(async subscription => {
|
||||||
console.log("send subscription to server...")
|
console.log("send subscription to server...")
|
||||||
|
|
||||||
let rawKey = subscription.getKey("p256dh")
|
let rawKey = subscription.getKey("p256dh")
|
||||||
let key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : ""
|
let key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : ""
|
||||||
|
|
||||||
let rawSecret = subscription.getKey("auth")
|
let rawSecret = subscription.getKey("auth")
|
||||||
let secret = rawSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawSecret))) : ""
|
let secret = rawSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawSecret))) : ""
|
||||||
|
|
||||||
let endpoint = subscription.endpoint
|
let endpoint = subscription.endpoint
|
||||||
|
|
||||||
let pushSubscription = {
|
let pushSubscription = {
|
||||||
endpoint,
|
endpoint,
|
||||||
p256dh: key,
|
p256dh: key,
|
||||||
@ -263,9 +263,9 @@ class MyServiceWorker {
|
|||||||
height: window.screen.height
|
height: window.screen.height
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let user = await fetch("/api/me").then(response => response.json())
|
let user = await fetch("/api/me").then(response => response.json())
|
||||||
|
|
||||||
return fetch("/api/pushsubscriptions/" + user.id + "/add", {
|
return fetch("/api/pushsubscriptions/" + user.id + "/add", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
credentials: "same-origin",
|
credentials: "same-origin",
|
||||||
@ -277,7 +277,7 @@ class MyServiceWorker {
|
|||||||
onNotificationClick(evt: NotificationEvent) {
|
onNotificationClick(evt: NotificationEvent) {
|
||||||
let notification = evt.notification
|
let notification = evt.notification
|
||||||
notification.close()
|
notification.close()
|
||||||
|
|
||||||
return (self as any).clients.matchAll().then(function(clientList) {
|
return (self as any).clients.matchAll().then(function(clientList) {
|
||||||
// If we have a link, use that link to open a new window.
|
// If we have a link, use that link to open a new window.
|
||||||
let url = notification.data
|
let url = notification.data
|
||||||
@ -329,7 +329,7 @@ class MyServiceWorker {
|
|||||||
installCache() {
|
installCache() {
|
||||||
// TODO: Implement a solution that caches resources with credentials: "same-origin"
|
// TODO: Implement a solution that caches resources with credentials: "same-origin"
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
|
|
||||||
// return caches.open(this.cache.version).then(cache => {
|
// return caches.open(this.cache.version).then(cache => {
|
||||||
// return cache.addAll([
|
// return cache.addAll([
|
||||||
// "./",
|
// "./",
|
||||||
@ -338,7 +338,7 @@ class MyServiceWorker {
|
|||||||
// ])
|
// ])
|
||||||
// })
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
fromCache(request) {
|
fromCache(request) {
|
||||||
return caches.open(this.cache.version).then(cache => {
|
return caches.open(this.cache.version).then(cache => {
|
||||||
return cache.match(request).then(matching => {
|
return cache.match(request).then(matching => {
|
||||||
@ -346,7 +346,7 @@ class MyServiceWorker {
|
|||||||
// console.log("Cache HIT:", request.url)
|
// console.log("Cache HIT:", request.url)
|
||||||
return Promise.resolve(matching)
|
return Promise.resolve(matching)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.reject("no-match")
|
return Promise.reject("no-match")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user