Skip to content

Commit 9ecf618

Browse files
authored
Merge pull request #10 from WorkofAditya/Alpha
Fix app update check and force cache refresh
2 parents 0c918be + 534e536 commit 9ecf618

2 files changed

Lines changed: 42 additions & 7 deletions

File tree

js/app.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -925,17 +925,43 @@ const REMOTE_VERSION_URL = "https://raw.githubusercontent.com/WorkofAditya/ChatB
925925

926926
let latestRemoteVersion = null;
927927

928+
function parseVersion(version) {
929+
return String(version)
930+
.trim()
931+
.split(".")
932+
.map((part) => Number.parseInt(part, 10) || 0);
933+
}
934+
935+
function isRemoteVersionNewer(remoteVersion, localVersion) {
936+
const remote = parseVersion(remoteVersion);
937+
const local = parseVersion(localVersion);
938+
const maxLen = Math.max(remote.length, local.length);
939+
940+
for (let i = 0; i < maxLen; i++) {
941+
const remotePart = remote[i] || 0;
942+
const localPart = local[i] || 0;
943+
944+
if (remotePart > localPart) return true;
945+
if (remotePart < localPart) return false;
946+
}
947+
948+
return false;
949+
}
950+
928951
function checkForAppUpdate() {
929952
fetch(REMOTE_VERSION_URL + "?cache=" + Date.now())
930-
.then(res => res.text())
953+
.then((res) => {
954+
if (!res.ok) throw new Error("Failed to fetch remote version");
955+
return res.text();
956+
})
931957
.then(remote => {
932958
const remoteVersion = remote.trim();
933959
latestRemoteVersion = remoteVersion;
934960

935961
const storedVersion = localStorage.getItem("updatedVersion");
936962
if (storedVersion === remoteVersion) return;
937963

938-
if (remoteVersion > LOCAL_VERSION) {
964+
if (isRemoteVersionNewer(remoteVersion, LOCAL_VERSION)) {
939965
showUpdatePopup();
940966
}
941967
})
@@ -951,10 +977,19 @@ function showUpdatePopup() {
951977
localStorage.setItem("updatedVersion", latestRemoteVersion);
952978

953979
if ("serviceWorker" in navigator) {
954-
const reg = await navigator.serviceWorker.getRegistration();
955-
if (reg && reg.waiting) {
956-
reg.waiting.postMessage("SKIP_WAITING");
957-
}
980+
const registrations = await navigator.serviceWorker.getRegistrations();
981+
982+
await Promise.all(
983+
registrations.map(async (reg) => {
984+
if (reg.waiting) reg.waiting.postMessage("SKIP_WAITING");
985+
await reg.update();
986+
})
987+
);
988+
}
989+
990+
if ("caches" in window) {
991+
const cacheNames = await caches.keys();
992+
await Promise.all(cacheNames.map((cacheName) => caches.delete(cacheName)));
958993
}
959994

960995
setTimeout(() => window.location.reload(), 300);

service-worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const APP_VERSION = "1";
1+
const APP_VERSION = "3";
22
const CACHE_NAME = `vault-cache-v${APP_VERSION}`;
33
const FILES_TO_CACHE = [
44
"/index.html",

0 commit comments

Comments
 (0)