forked from rDrama/rDrama
go back to CRGD's cervix worker
parent
93f189c03b
commit
596cf967a2
|
@ -406,7 +406,7 @@ function sendFormXHRSwitch(e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("serviceWorker" in navigator) {
|
if ("serviceWorker" in navigator) {
|
||||||
navigator.serviceWorker.register("/service-worker.js?v=2")
|
navigator.serviceWorker.register("/service-worker.js?v=3")
|
||||||
.then((registration) => registration.update())
|
.then((registration) => registration.update())
|
||||||
.catch((e) => console.log("Service worker update failed with error", e));
|
.catch((e) => console.log("Service worker update failed with error", e));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,52 +1,50 @@
|
||||||
importScripts("https://js.pusher.com/beams/service-worker.js");
|
importScripts("https://js.pusher.com/beams/service-worker.js");
|
||||||
|
|
||||||
const CACHE = "pwabuilder-offline-page";
|
// offline static page handler
|
||||||
|
// @crgd
|
||||||
|
|
||||||
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
|
const CACHE_NAME = "offlineCache-v1";
|
||||||
|
const OFFLINE_URL = "/assets/offline.html";
|
||||||
|
|
||||||
const offlineFallbackPage = "/assets/offline.html";
|
self.addEventListener("install", (event) => {
|
||||||
|
const cacheOfflinePage = async () => {
|
||||||
|
const cache = await caches.open(CACHE_NAME);
|
||||||
|
await cache.add(new Request(OFFLINE_URL, {cache: "reload"}));
|
||||||
|
};
|
||||||
|
|
||||||
self.addEventListener("message", (event) => {
|
cacheOfflinePage().then(() => {
|
||||||
if (event.data && event.data.type === "SKIP_WAITING") {
|
this.skipWaiting();
|
||||||
self.skipWaiting();
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('install', async (event) => {
|
self.addEventListener("activate", (event) => {
|
||||||
|
const expectedCaches = [CACHE_NAME];
|
||||||
|
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.open(CACHE)
|
caches.keys().then(keys => Promise.all(
|
||||||
.then((cache) => cache.add(offlineFallbackPage))
|
keys.map(key => {
|
||||||
|
if (!expectedCaches.includes(key)) {
|
||||||
|
return caches.delete(key);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (workbox.navigationPreload.isSupported()) {
|
self.addEventListener("fetch", (event) => {
|
||||||
workbox.navigationPreload.enable();
|
if (event.request.mode === "navigate") {
|
||||||
}
|
|
||||||
|
|
||||||
workbox.routing.registerRoute(
|
|
||||||
new RegExp('/*'),
|
|
||||||
new workbox.strategies.StaleWhileRevalidate({
|
|
||||||
cacheName: CACHE
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
self.addEventListener('fetch', (event) => {
|
|
||||||
if (event.request.mode === 'navigate') {
|
|
||||||
event.respondWith((async () => {
|
event.respondWith((async () => {
|
||||||
try {
|
try {
|
||||||
const preloadResp = await event.preloadResponse;
|
const preloadResponse = await event.preloadResponse;
|
||||||
|
if (preloadResponse) return preloadResponse;
|
||||||
|
|
||||||
if (preloadResp) {
|
const networkResponse = await fetch(event.request);
|
||||||
return preloadResp;
|
return networkResponse;
|
||||||
}
|
|
||||||
|
|
||||||
const networkResp = await fetch(event.request);
|
|
||||||
return networkResp;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log("Fetch failed; returning offline page instead.", error);
|
||||||
|
|
||||||
const cache = await caches.open(CACHE);
|
const cachedResponse = await caches.match(OFFLINE_URL);
|
||||||
const cachedResp = await cache.match(offlineFallbackPage);
|
return cachedResponse;
|
||||||
return cachedResp;
|
|
||||||
}
|
}
|
||||||
})());
|
})());
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue