From b4191eebb29ae0a0ebc7e09e135e5452258fb025 Mon Sep 17 00:00:00 2001 From: vickkie Date: Wed, 28 Feb 2024 11:51:13 +0300 Subject: [PATCH] clarityy --- index.html | 33 +++++++++++----------- offline-uzitrake.js | 68 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 73 insertions(+), 28 deletions(-) diff --git a/index.html b/index.html index ecb94ba5..486fc453 100755 --- a/index.html +++ b/index.html @@ -8,7 +8,6 @@ - @@ -43,6 +42,21 @@ + + @@ -1516,7 +1530,7 @@

- + @@ -1541,20 +1555,5 @@

gtag("config", "G-F0NF6J8B0H"); --> - - diff --git a/offline-uzitrake.js b/offline-uzitrake.js index c5599716..d57dfea9 100755 --- a/offline-uzitrake.js +++ b/offline-uzitrake.js @@ -1,8 +1,5 @@ const cacheName = "uzitrake-offline"; var cacheVersion = 2; -// var cacheName = { -// offline: 'uzitrake-offline' + cacheVersion, -// } let offlineUrl = "offline.html"; @@ -46,14 +43,63 @@ self.addEventListener("activate", (e) => { ); }); +self.addEventListener("fetch", (e) => { + e.respondWith( + fetch(e.request) + .then((res) => { + // Check if the response status is OK (status code 200) + if (res.status === 200) { + // Check if the request is not from a chrome-extension + if (!e.request.url.startsWith("chrome-extension://")) { + // Make a clone of the response + const resClone = res.clone(); + // Open the cache and add the response to it + caches.open(cacheName).then((cache) => { + cache.put(e.request, resClone); + }); + } + } + // Return the original response + return res; + }) + .catch((err) => + caches + .match(e.request) + .then((res) => res) + .then(console.log("Error:", err)) + ) + ); +}); + // call fetch event self.addEventListener("fetch", (e) => { - // Check if the request is for the Clarity script - if (e.request.url.includes("clarity.ms")) { - // If it is, try to fetch the resource from the network - e.respondWith(fetch(e.request)); - } else { - // If it's not, serve the offline.html page from the cache - e.respondWith(fetch(e.request).catch(() => caches.match(offlineUrl))); - } + // console.log('Service worker: fetching'); + e.respondWith(fetch(e.request).catch(() => caches.match(offlineUrl))); }); + +// call fetch event +// self.addEventListener("fetch", (e) => { +// // Check if the request is for the Clarity script +// if (e.request.url.includes("clarity.ms")) { +// // If it is, try to fetch the resource from the network +// e.respondWith(fetch(e.request)); +// } else { +// // If it's not, serve the offline.html page from the cache +// e.respondWith(fetch(e.request).catch(() => caches.match(offlineUrl))); +// } +// }); + +// self.addEventListener("fetch", (e) => { +// if (e.request.url.includes("clarity.ms")) { +// // Attempt to fetch the resource from the network +// e.respondWith( +// fetch(e.request).catch(() => { +// // If the network request fails, serve a fallback or offline page +// return caches.match("fallback.html"); +// }) +// ); +// } else { +// // For other requests, serve from cache or network as before +// e.respondWith(fetch(e.request).catch(() => caches.match(offlineUrl))); +// } +// });