cache qrcode reader

This commit is contained in:
Corentin Sechet 2024-03-01 15:50:09 +01:00
parent 47f925d62c
commit 2439bb8e05
2 changed files with 26 additions and 0 deletions

View File

@ -24,6 +24,13 @@ self.addEventListener('message', async (event) => {
tallyUrls.add(tallyUrl)
refreshTally(tallyUrl)
}
const cacheUrl = event.data.cacheUrl
if (cacheUrl) {
event.waitUntil(cacheReader([
cacheUrl
]))
}
} catch (error) {
console.log(error)
}
@ -34,6 +41,12 @@ self.addEventListener('fetch', (event) => {
if (tallyUrls.has(event.request.url)) {
event.respondWith(tally(event.request))
}
else {
const match = caches.match(event.request)
if(match) {
event.respondWith(match)
}
}
} catch (error) {
console.log(error)
}
@ -180,3 +193,15 @@ function getPendingEvents () {
request.onerror = () => reject(request.error)
})
}
async function cacheReader (readerUrl) {
const cache = await caches.open("v1")
await cache.addAll([
readerUrl,
"/static/qrcode/css/style.css",
"/static/qrcode/js/qrcode-reader.js",
"/static/qrcode/js/nacl.min.js",
"/static/qrcode/js/zxing-browser.min.js",
])
}

View File

@ -41,6 +41,7 @@
scope: "/",
})
const registration = await navigator.serviceWorker.ready
registration.active.postMessage({cacheUrl: window.location.pathname})
function refreshTally() {
if (document.hidden === false) {
registration.active.postMessage({refreshTally: "{{ tally_url }}"})