try to make install work by caching

This commit is contained in:
creeper123123321 2021-05-30 17:34:35 -03:00
parent 4aec1e0458
commit 29375aecd0
3 changed files with 41 additions and 4 deletions

View File

@ -40,4 +40,4 @@ function authNotification(msg, yes, no) {
};
setTimeout(() => { delete notificationCallbacks[tag] }, 30 * 1000);
});
}
}

View File

@ -13,7 +13,13 @@ window.addEventListener('beforeinstallprompt', e => {
// On load
$(() => {
if (navigator.serviceWorker) {
navigator.serviceWorker.register("sw.js");
navigator.serviceWorker.register("sw.js")
.then(it => it.installing.postMessage({
action: "cache",
urls: performance.getEntriesByType("resource")
.map(it => it.name)
.filter(it => it.endsWith(".js") || it.endsWith(".css") || it.endsWith(".png"))
})); // https://stackoverflow.com/questions/46830493/is-there-any-way-to-cache-all-files-of-defined-folder-path-in-service-worker
}
ohNo();

View File

@ -6,6 +6,37 @@ self.addEventListener("notificationclick", event => {
viac.postMessage({tag: event.notification.tag, action: event.action});
});
addEventListener("fetch", e => {
// chrome please show "add to home screen"
// stolen from https://github.com/mozilla/serviceworker-cookbook/blob/master/strategy-network-or-cache/service-worker.js (MIT license)
var CACHE = "network-or-cache";
self.addEventListener("install", evt => {
evt.waitUntil(cache(["./"]));
});
self.addEventListener("fetch", evt => {
evt.respondWith(
fromNetwork(evt.request)
.catch(() => fromCache(evt.request))
);
});
addEventListener("message", e => {
if (e.data.action == "cache") {
event.waitUntil(cache(e.data.urls));
}
});
function cache(urls) {
return caches.open(CACHE).then(cache => cache.addAll(urls));
}
function fromNetwork(request) {
return fetch(request);
}
function fromCache(request) {
return caches.open(CACHE)
.then(cache => cache.match(request))
.then(matching => matching || Promise.reject("no-match"));
}