mirror of
https://github.com/ViaVersion/VIAaaS.git
synced 2025-01-09 19:48:37 +01:00
try to make install work by caching
This commit is contained in:
parent
4aec1e0458
commit
29375aecd0
@ -40,4 +40,4 @@ function authNotification(msg, yes, no) {
|
||||
};
|
||||
setTimeout(() => { delete notificationCallbacks[tag] }, 30 * 1000);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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"));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user