We use ASI

This commit is contained in:
Neale Pickett 2019-11-04 16:22:44 +00:00
parent 54ff708c5b
commit b5b578e08a
3 changed files with 58 additions and 58 deletions

View File

@ -2,15 +2,15 @@ function pwa_init() {
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
navigator.serviceWorker.register("./sw.js").then(function(reg) { navigator.serviceWorker.register("./sw.js").then(function(reg) {
}).catch(function(err) { }).catch(function(err) {
console.warn("Error while registering service worker", err); console.warn("Error while registering service worker", err)
}); })
} else { } else {
console.log("Service workers not supported. Some offline functionality may not work"); console.log("Service workers not supported. Some offline functionality may not work")
} }
} }
if (document.readyState === "loading") { if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", pwa_init); document.addEventListener("DOMContentLoaded", pwa_init)
} else { } else {
pwa_init(); pwa_init()
} }

View File

@ -109,68 +109,68 @@ function showPuzzles(teamId) {
} }
function drawCacheButton(teamId) { function drawCacheButton(teamId) {
let cacher = document.createElement("li"); let cacher = document.createElement("li")
let cache_button = document.createElement("a"); let cache_button = document.createElement("a")
cache_button.innerText = "Cache"; cache_button.innerText = "Cache"
cache_button.title = "Cache an offine copy of current content"; cache_button.title = "Cache an offine copy of current content"
cache_button.href = "#"; cache_button.href = "#"
cache_button.addEventListener("click", async function() { cache_button.addEventListener("click", async function() {
toast("Caching all currently-open content"); toast("Caching all currently-open content")
await fetchAll(teamId); await fetchAll(teamId)
toast("Done caching content"); toast("Done caching content")
}); })
cacher.appendChild(cache_button); cacher.appendChild(cache_button)
document.getElementsByTagName("nav")[0].getElementsByTagName("ul")[0].appendChild(cacher); document.getElementsByTagName("nav")[0].getElementsByTagName("ul")[0].appendChild(cacher)
function updateCacheButton() { function updateCacheButton() {
let headers = new Headers(); let headers = new Headers()
headers.append("pragma", "no-cache"); headers.append("pragma", "no-cache")
headers.append("cache-control", "no-cache"); headers.append("cache-control", "no-cache")
fetch("current_manifest.json?id=" + teamId, {method: "HEAD", headers: headers}) fetch("current_manifest.json?id=" + teamId, {method: "HEAD", headers: headers})
.then( resp => { .then( resp => {
if (resp.ok) { if (resp.ok) {
cacher.style.disply = "initial"; cacher.style.disply = "initial"
} else { } else {
cacher.style.display = "none"; cacher.style.display = "none"
} }
}).catch( ex => { }).catch( ex => {
cacher.style.display = "none"; cacher.style.display = "none"
}) })
} }
setInterval ( updateCacheButton , 30000); setInterval ( updateCacheButton , 30000)
updateCacheButton(); updateCacheButton()
} }
async function fetchAll(teamId) { async function fetchAll(teamId) {
let headers = new Headers(); let headers = new Headers()
headers.append("pragma", "no-cache"); headers.append("pragma", "no-cache")
headers.append("cache-control", "no-cache"); headers.append("cache-control", "no-cache")
requests = []; requests = []
requests.push( fetch("current_manifest.json?id=" + teamId, {headers: headers}) requests.push( fetch("current_manifest.json?id=" + teamId, {headers: headers})
.then( resp => { .then( resp => {
if (resp.ok) { if (resp.ok) {
resp.json() resp.json()
.then( contents => { .then( contents => {
console.log("Processing manifest"); console.log("Processing manifest")
for (let resource of contents) { for (let resource of contents) {
if (resource == "puzzles.json") { if (resource == "puzzles.json") {
continue; continue
} }
fetch(resource) fetch(resource)
.then( e => { .then( e => {
console.log("Fetched " + resource); console.log("Fetched " + resource)
}) })
} }
}) })
} }
})); }))
let resp = await fetch("puzzles.json?id=" + teamId, {headers: headers}); let resp = await fetch("puzzles.json?id=" + teamId, {headers: headers})
if (resp.ok) { if (resp.ok) {
let categories = await resp.json(); let categories = await resp.json()
let cat_names = Object.keys(categories) let cat_names = Object.keys(categories)
cat_names.sort() cat_names.sort()
for (let cat_name of cat_names) { for (let cat_name of cat_names) {
@ -178,17 +178,17 @@ async function fetchAll(teamId) {
// Skip metadata // Skip metadata
continue continue
} }
let puzzles = categories[cat_name]; let puzzles = categories[cat_name]
for (let puzzle of puzzles) { for (let puzzle of puzzles) {
let url = "puzzle.html?cat=" + cat_name + "&points=" + puzzle[0] + "&pid=" + puzzle[1]; let url = "puzzle.html?cat=" + cat_name + "&points=" + puzzle[0] + "&pid=" + puzzle[1]
requests.push( fetch(url) requests.push( fetch(url)
.then( e => { .then( e => {
console.log("Fetched " + url); console.log("Fetched " + url)
})); }))
} }
} }
} }
return Promise.all(requests); return Promise.all(requests)
} }
function login(e) { function login(e) {
@ -240,8 +240,8 @@ function init() {
} }
if (document.readyState === "loading") { if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init); document.addEventListener("DOMContentLoaded", init)
} else { } else {
init(); init()
} }

View File

@ -8,42 +8,42 @@ var content = [
"moth.js", "moth.js",
"sw.js", "sw.js",
"points.json", "points.json",
]; ]
self.addEventListener("install", function(e) { self.addEventListener("install", function(e) {
e.waitUntil( e.waitUntil(
caches.open(cacheName).then(function(cache) { caches.open(cacheName).then(function(cache) {
return cache.addAll(content).then( return cache.addAll(content).then(
function() { function() {
self.skipWaiting(); self.skipWaiting()
});
}) })
); })
}); )
})
/* Attempt to fetch live resources, first, then fall back to cache */ /* Attempt to fetch live resources, first, then fall back to cache */
self.addEventListener('fetch', function(event) { self.addEventListener('fetch', function(event) {
let cache_used = false; let cache_used = false
event.respondWith( event.respondWith(
fetch(event.request) fetch(event.request)
.catch(function(evt) { .catch(function(evt) {
//console.log("Falling back to cache for " + event.request.url); //console.log("Falling back to cache for " + event.request.url)
cache_used = true; cache_used = true
return caches.match(event.request, {ignoreSearch: true}); return caches.match(event.request, {ignoreSearch: true})
}).then(function(res) { }).then(function(res) {
if (res && res.ok) { if (res && res.ok) {
let res_clone = res.clone(); let res_clone = res.clone()
if (! cache_used && event.request.method == "GET" ) { if (! cache_used && event.request.method == "GET" ) {
caches.open(cacheName).then(function(cache) { caches.open(cacheName).then(function(cache) {
cache.put(event.request, res_clone); cache.put(event.request, res_clone)
//console.log("Storing " + event.request.url + " in cache"); //console.log("Storing " + event.request.url + " in cache")
}); })
} }
return res; return res
} else { } else {
console.log("Failed to retrieve resource"); console.log("Failed to retrieve resource")
} }
}) })
); )
}); })