diff --git a/build.js b/build.js index a4ffccb..00cd44e 100644 --- a/build.js +++ b/build.js @@ -12,7 +12,7 @@ fs.cpSync("./static/", "./build/", { recursive: true }); fs.cpSync("./assets/", "./build/assets/", { recursive: true }); const buildTimestamp = Date.now().toString(); fs.writeFileSync("./build/index.html", fs.readFileSync("./build/index.html").toString().replace(/buildTimestamp/g, buildTimestamp)); -fs.writeFileSync("./build/sw.js", fs.readFileSync("./build/sw.js").toString().replace("buildTimestamp", buildTimestamp)); +fs.writeFileSync("./build/sw2.js", fs.readFileSync("./build/sw2.js").toString().replace("buildTimestamp", buildTimestamp)); const buildClientCode = () => /** @type {Promise} */(new Promise((resolve, reject) => { console.log("Building client code..."); diff --git a/src/main.js b/src/main.js index 5ae24e3..54b848a 100644 --- a/src/main.js +++ b/src/main.js @@ -1,17 +1,6 @@ import versionData from '../version.json'; const { version, lastUpdated } = versionData; -if ("serviceWorker" in navigator) { - navigator.serviceWorker.addEventListener("message", (e) => { - const message = e.data; - if (message.event === "activate" && buildTimestamp !== message.version) { - // worker was updated in the background - document.getElementById("updateNotification").style.display = "block"; - } - }); - navigator.serviceWorker.register("./sw.js"); -} - import settingsManager from './settings.js'; import { clanFilter, leaderboardFilter } from "./clanFilters.js"; import WindowManager from "./windowManager.js"; diff --git a/static/index.html b/static/index.html index 36fd74b..c50c13f 100644 --- a/static/index.html +++ b/static/index.html @@ -1,8 +1,9 @@ + - - + - - FX Client - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + + + - + + \ No newline at end of file diff --git a/static/sw.js b/static/sw.js index c79bfe5..98586bc 100644 --- a/static/sw.js +++ b/static/sw.js @@ -1,47 +1 @@ -const cacheName = "buildTimestamp"; // this gets replaced by the build script - -self.addEventListener("install", (e) => { - console.log("[Service Worker] Install"); - self.skipWaiting(); -}); - -self.addEventListener("fetch", (e) => { - const url = e.request.url; - // Cache http and https only, skip unsupported chrome-extension:// and file://... - if (!(url.startsWith('http:') || url.startsWith('https:'))) { - return; - } - e.respondWith( - (async () => { - const r = await caches.match(e.request); - console.log(`[Service Worker] Fetching resource: ${url}`); - if (r) { - return r; - } - const response = await fetch(e.request); - const cache = await caches.open(cacheName); - console.log(`[Service Worker] Caching new resource: ${url}`); - cache.put(e.request, response.clone()); - return response; - })(), - ); -}); - -self.addEventListener("activate", (e) => { - console.log("[Service Worker] Activated", cacheName); - self.clients.matchAll().then(clients => { - clients.forEach(client => client.postMessage({ event: "activate", version: cacheName })); - }); - e.waitUntil( - caches.keys().then((keyList) => { - return Promise.all( - keyList.map((key) => { - if (key === cacheName) { - return; - } - return caches.delete(key); - }), - ); - }), - ); -}); +// removed \ No newline at end of file diff --git a/static/sw2.js b/static/sw2.js new file mode 100644 index 0000000..0d4794c --- /dev/null +++ b/static/sw2.js @@ -0,0 +1,51 @@ +const cacheName = "buildTimestamp"; // this gets replaced by the build script + +self.addEventListener("install", (e) => { + console.log("[Service Worker] Install", cacheName); +}); +self.addEventListener('message', function (e) { + if (e.data.update) { + self.skipWaiting(); + } +}); + +self.addEventListener("fetch", (e) => { + const url = e.request.url; + // Cache http and https only, skip unsupported chrome-extension:// and file://... + if (!(url.startsWith('http:') || url.startsWith('https:'))) { + return; + } + e.respondWith( + (async () => { + const cache = await caches.open(cacheName); + const r = await cache.match(e.request); + console.log(`[Service Worker] Fetching resource: ${url}`); + if (r) { + return r; + } + const response = await fetch(e.request); + console.log(`[Service Worker] Caching new resource: ${url}`); + cache.put(e.request, response.clone()); + return response; + })(), + ); + self.clients.matchAll().then(clients => { + clients.forEach(client => client.postMessage({ event: "activate", version: cacheName })); + }); +}); + +self.addEventListener("activate", (e) => { + console.log("[Service Worker] Activated", cacheName); + e.waitUntil( + caches.keys().then((keyList) => { + return Promise.all( + keyList.map((key) => { + if (key === cacheName) { + return; + } + return caches.delete(key); + }), + ); + }), + ); +});