From 51bac6f65f5bf1929395aa8b2b923207c7499c7f Mon Sep 17 00:00:00 2001 From: sky Date: Wed, 29 Oct 2025 20:29:39 -0400 Subject: [PATCH] caching service worker --- js/main.js | 4 ++++ sw.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 sw.js diff --git a/js/main.js b/js/main.js index 17095c3..9b98b3e 100755 --- a/js/main.js +++ b/js/main.js @@ -1,5 +1,6 @@ let sAlert = window.parent.sAlert; + function setBackground() { let randomNumbers = [100-Math.abs(Math.floor(Math.random() * 200) - 99), 100-Math.abs(Math.floor(Math.random() * 200) - 99)]; document.body.style.setProperty("--gradient-x", randomNumbers[0] + "%"); @@ -54,6 +55,9 @@ function setTheme(arg) { `) } document.addEventListener("DOMContentLoaded", ()=>{ + if("serviceWorker" in navigator) { + navigator.serviceWorker.register("/sw.js", { scope: '/' }); + } if(!(localStorage.getItem("selenite.fast-mode") == 'true')) { setBackground(); loadPJS(); diff --git a/sw.js b/sw.js new file mode 100644 index 0000000..e42c2fe --- /dev/null +++ b/sw.js @@ -0,0 +1,46 @@ +importScripts( + 'https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js' +); + +if(workbox) { + self.skipWaiting(); + workbox.core.clientsClaim(); + + workbox.routing.registerRoute( + /\.(?:png|gif|jpg|jpeg|svg|webp|avif|gif)$/, + new workbox.strategies.CacheFirst({ + cacheName: 'images-cache', + plugins: [ + new workbox.expiration.ExpirationPlugin({ + maxEntries: 500, + maxAgeSeconds: 365 * 24 * 60 * 60, + }), + ], + }) + ); + workbox.routing.registerRoute( + /.*resources\/(semag|sppa).*/, + new workbox.strategies.CacheFirst({ + cacheName: 'resources-cache', + plugins: [ + new workbox.expiration.ExpirationPlugin({ + maxEntries: 100, + maxAgeSeconds: 365 * 24 * 60 * 60, + }), + ], + }) + ); + + workbox.routing.registerRoute( + /.*resources\/(games|apps)\.json$/, + new workbox.strategies.StaleWhileRevalidate({ + cacheName: 'json-cache', + plugins: [ + new workbox.expiration.ExpirationPlugin({ + maxAgeSeconds: 24 * 60 * 60, + maxEntries: 2, + }), + ], + }) + ); +}; \ No newline at end of file