Skip to content

Commit 441c177

Browse files
authored
Merge pull request #2 from WorkofAditya/Beta
Added version update system
2 parents c16cd9d + ab58f13 commit 441c177

5 files changed

Lines changed: 118 additions & 17 deletions

File tree

index.html

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<div class="vault-container">
1919
<h1>Chatbot</h1>
2020
<div class="vault-toolbar">
21-
<button id="openChangelogBtn" class="change-btn">Changelog</button>
2221
<button id="editDocsBtn" type="button" title="Edit Documents">Manage</button>
2322
<button id="aboutBtn" class="toolbar-btn">About</button>
2423
<button id="helpBtn">?</button>
@@ -85,11 +84,13 @@ <h2 class="about-title">ChatBot</h2>
8584
<p class="about-desc">A secure offline chatbot vault that stores your personal documents, notes, and files locally, no internet or servers required. <b>made to keep your data truly <i>yours</i>.</b></p>
8685

8786
<div class="about-links-row">
88-
<a href="https://github.com/WorkofAditya/ChatBot" target="_blank">GitHub</a>
89-
<a href="https://codepen.io/WorkofAditya/pen/azNBRWr" target="_blank">CodePen</a>
90-
</div>
87+
<a href="https://github.com/WorkofAditya/ChatBot" target="_blank">GitHub</a>
88+
<a href="https://codepen.io/WorkofAditya/pen/azNBRWr" target="_blank">CodePen</a>
89+
<a href="#" id="openChangelogBtn">Changelog</a>
90+
</div>
91+
9192
<p class="about-dev">Developed by <b>Adityasinh Sodha</b></p>
92-
<p class="about-version">Version: <b>v2.0.0</b></p>
93+
<p class="about-version">Version: <b>v2.0.1</b></p>
9394
</div>
9495
</div>
9596

@@ -125,6 +126,17 @@ <h2 class="about-title">User Guide</h2>
125126
</div>
126127
</div>
127128

129+
<!-- UPDATE POPUP -->
130+
<div id="updatePopup" class="popup">
131+
<div class="popup-content">
132+
<h2>Update Available</h2>
133+
<p>A new version of ChatBot is ready. Install the update to get new features and improvements.</p>
134+
<div class="popup-actions">
135+
<button id="refreshAppBtn">Update Now</button>
136+
<button id="dismissUpdateBtn">Later</button>
137+
</div>
138+
</div>
139+
</div>
128140

129141
<script src="js/app.js"></script>
130142
</body>

js/app.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,3 +930,50 @@ helpBtn.onclick = async () => {
930930
closeHelp.onclick = () => {
931931
helpPopup.style.display = "none";
932932
};
933+
934+
const LOCAL_VERSION = "2";
935+
const REMOTE_VERSION_URL = "https://raw.githubusercontent.com/WorkofAditya/ChatBot/refs/heads/Beta/version.txt";
936+
937+
let latestRemoteVersion = null;
938+
939+
function checkForAppUpdate() {
940+
fetch(REMOTE_VERSION_URL + "?cache=" + Date.now())
941+
.then(res => res.text())
942+
.then(remote => {
943+
const remoteVersion = remote.trim();
944+
latestRemoteVersion = remoteVersion;
945+
946+
const storedVersion = localStorage.getItem("updatedVersion");
947+
if (storedVersion === remoteVersion) return;
948+
949+
if (remoteVersion > LOCAL_VERSION) {
950+
showUpdatePopup();
951+
}
952+
})
953+
.catch(() => {});
954+
}
955+
956+
function showUpdatePopup() {
957+
const popup = document.getElementById("updatePopup");
958+
popup.style.display = "flex";
959+
960+
document.getElementById("refreshAppBtn").onclick = async () => {
961+
popup.style.display = "none";
962+
localStorage.setItem("updatedVersion", latestRemoteVersion);
963+
964+
if ("serviceWorker" in navigator) {
965+
const reg = await navigator.serviceWorker.getRegistration();
966+
if (reg && reg.waiting) {
967+
reg.waiting.postMessage("SKIP_WAITING");
968+
}
969+
}
970+
971+
setTimeout(() => window.location.reload(), 300);
972+
};
973+
974+
document.getElementById("dismissUpdateBtn").onclick = () => {
975+
popup.style.display = "none";
976+
};
977+
}
978+
979+
if (navigator.onLine) checkForAppUpdate();

service-worker.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const CACHE_NAME = "vault-cache-v14";
1+
const APP_VERSION = "1";
2+
const CACHE_NAME = `vault-cache-v${APP_VERSION}`;
23
const FILES_TO_CACHE = [
3-
"/",
44
"/index.html",
55
"/styles.css",
66
"/js/app.js",
@@ -13,13 +13,10 @@ const FILES_TO_CACHE = [
1313
"/icons/favicon.ico"
1414
];
1515

16-
1716
// Install: cache everything
1817
self.addEventListener("install", (event) => {
1918
event.waitUntil(
20-
caches.open(CACHE_NAME).then((cache) => {
21-
return cache.addAll(FILES_TO_CACHE);
22-
})
19+
caches.open(CACHE_NAME).then((cache) => cache.addAll(FILES_TO_CACHE))
2320
);
2421
self.skipWaiting();
2522
});
@@ -30,21 +27,40 @@ self.addEventListener("activate", (event) => {
3027
caches.keys().then((keyList) =>
3128
Promise.all(
3229
keyList.map((key) => {
33-
if (key !== CACHE_NAME) {
34-
return caches.delete(key);
35-
}
30+
if (key !== CACHE_NAME) return caches.delete(key);
3631
})
3732
)
3833
)
3934
);
4035
self.clients.claim();
4136
});
4237

43-
// Fetch: serve from cache when offline
38+
// Fetch: cache-first for offline
4439
self.addEventListener("fetch", (event) => {
40+
if (event.request.method !== "GET") return;
41+
4542
event.respondWith(
46-
caches.match(event.request).then((response) => {
47-
return response || fetch(event.request);
43+
caches.match(event.request, { ignoreSearch: true }).then((cached) => {
44+
if (cached) return cached;
45+
46+
return fetch(event.request)
47+
.then((response) => {
48+
const clone = response.clone();
49+
caches.open(CACHE_NAME).then((cache) => {
50+
cache.put(event.request, clone);
51+
});
52+
return response;
53+
})
54+
.catch(() => {
55+
if (event.request.mode === "navigate") {
56+
return caches.match("/index.html");
57+
}
58+
});
4859
})
4960
);
5061
});
62+
63+
// Force update activation from app.js
64+
self.addEventListener("message", (event) => {
65+
if (event.data === "SKIP_WAITING") self.skipWaiting();
66+
});

styles.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,3 +1128,28 @@ hr {
11281128
margin: 16px 0;
11291129
height: 2px;
11301130
}
1131+
1132+
#updatePopup .popup-content {
1133+
max-width: 380px;
1134+
text-align: center;
1135+
padding: 28px 22px;
1136+
}
1137+
#updatePopup h2 {
1138+
font-size: 1.4rem;
1139+
color: #6ab7ff;
1140+
margin-bottom: 10px;
1141+
}
1142+
#updatePopup p {
1143+
font-size: 0.94rem;
1144+
color: #e1e7f8;
1145+
margin-bottom: 14px;
1146+
}
1147+
#updatePopup .popup-actions button {
1148+
background: rgba(60, 110, 200, 0.15);
1149+
border: 1px solid rgba(60, 110, 200, 0.4);
1150+
color: #9abfff;
1151+
}
1152+
#updatePopup .popup-actions button:hover {
1153+
background: rgba(60, 110, 200, 0.24);
1154+
color: #fff;
1155+
}

version.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2

0 commit comments

Comments
 (0)