diff --git a/shared/OctoHub-Global.js b/shared/OctoHub-Global.js index bda5d55..7d74831 100644 --- a/shared/OctoHub-Global.js +++ b/shared/OctoHub-Global.js @@ -1,6 +1,10 @@ window.addEventListener('load', (function(){ -if (!['', 'hub.octt.eu.org'].includes(location.host)) { +if (['', 'hub.octt.eu.org'].includes(location.host)) { + if ('serviceWorker' in navigator) { + navigator.serviceWorker.register('/ServiceWorker.js'); + } +} else { var noticeElem = document.createElement('p'); noticeElem.style = ` position: fixed; diff --git a/static/ServiceWorker.js b/static/ServiceWorker.js index e69de29..93f9c81 100644 --- a/static/ServiceWorker.js +++ b/static/ServiceWorker.js @@ -0,0 +1,39 @@ +const cachables = { + "/": "networkFirst", + "//": "networkFirst", +}; + +const checkUrlCaching = (url) => (cachables[url] || cachables[`/${url.split('://').slice(1).join('://').split('/')[1]}/`]); + +const putResponseInCache = (request, response) => { + if (response.ok) { + caches.open('OctoSpaccHub/v1').then((cache) => cache.put(request, response.clone())); + } +} + +const strategies = { + networkFirst: async (request) => { + try { + const networkResponse = await fetch(request); + putResponseInCache(request, networkResponse); + return networkResponse; + } catch (error) { + return ((await caches.match(request)) || Response.error()); + } + }, + cacheFirst: async (request) => { + const fetchResponsePromise = fetch(request).then(async (networkResponse) => { + putResponseInCache(request, networkResponse); + return networkResponse; + }); + return (await caches.match(request)) || (await fetchResponsePromise); + }, +} + +self.addEventListener('activate', () => self.clients.claim()); +self.addEventListener('fetch', (event) => { + const strategy = strategies[checkUrlCaching(event.request.url)]; + if (strategy) { + return event.respondWith(strategy(event.request)); + } +}); \ No newline at end of file diff --git a/static/index.html b/static/index.html index 97bb2e0..795252a 100644 --- a/static/index.html +++ b/static/index.html @@ -26,6 +26,11 @@
At the moment, it's still kind of an humble hub...
(But every once in a while I do make a new link appear below!)
+
+
+ +

Site Theme Song: Spirit of Famiclone Selection Menu

+