Skip to content

Commit ee1da86

Browse files
hiteshkrmsftCopilot
andcommitted
Add BUILD_ID console log + network-first SW for index.html
Tells us at a glance whether the latest deployed JS is actually running -- main.js logs [Aadhat] BUILD <id> on startup and exposes window.__AADHAT_BUILD__ so the user can paste it back to us. Also fixes the root cause of stale-cache reports: the SW used cache-first for index.html, so on the first reload after a deploy the user got a stale index.html that still referenced the old main.js?v=YYYYMMDD URL -- guaranteeing they saw the old code regardless of cache version bumps. SW now uses network-first for navigation requests; static assets remain cache-first with background refresh. Bumps cache-buster query strings (v=20260606b) and SW cache (aadhat-v5) as well so any current stale cache is invalidated. 123/123 tests still pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 137c439 commit ee1da86

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

www/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<!-- Favicon -->
1212
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><rect fill='%23667eea' width='100' height='100' rx='20'/><text x='50' y='68' font-size='50' text-anchor='middle' fill='white' font-family='Arial,sans-serif' font-weight='bold'>A</text></svg>">
1313

14-
<link rel="stylesheet" href="styles.css?v=20260107">
14+
<link rel="stylesheet" href="styles.css?v=20260606b">
1515

1616
<!-- Firebase SDK -->
1717
<script src="https://www.gstatic.com/firebasejs/10.7.1/firebase-app-compat.js"></script>
@@ -42,7 +42,7 @@
4242
</script>
4343

4444
<!-- ES6 Modular Architecture (loads and injects all templates) -->
45-
<script type="module" src="js/main.js?v=20260107"></script>
45+
<script type="module" src="js/main.js?v=20260606b"></script>
4646

4747
</body>
4848
</html>

www/js/main.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ import { AdminManager } from './modules/admin.js';
3030
import { TemplateLoader } from './utils/template-loader.js';
3131
import { Helpers } from './utils/helpers.js';
3232

33+
// Build stamp — bumped on every deploy that ships new JS so you can verify
34+
// in the browser console whether the latest code is actually running. If
35+
// your console shows an OLDER BUILD_ID after a deploy + reload, the service
36+
// worker / browser is still serving stale assets (reload twice, or hard
37+
// refresh / unregister the service worker).
38+
const BUILD_ID = '20260606-negstock-rate-fix';
39+
console.log(
40+
`%c[Aadhat] BUILD ${BUILD_ID}`,
41+
'color:#fff;background:#0a7;font-weight:bold;padding:2px 6px;border-radius:3px'
42+
);
43+
// Expose on window so the user can copy/paste it back to us when debugging.
44+
window.__AADHAT_BUILD__ = BUILD_ID;
45+
3346
// Load and inject HTML templates from separate .html files
3447
async function injectTemplates() {
3548
const templates = await TemplateLoader.loadAllTemplates();

www/service-worker.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Provides offline functionality and caching
44
*/
55

6-
const CACHE_NAME = 'aadhat-v4';
6+
const CACHE_NAME = 'aadhat-v5';
77
const STATIC_ASSETS = [
88
'/',
99
'/index.html',
@@ -114,7 +114,14 @@ self.addEventListener('activate', (event) => {
114114

115115
/**
116116
* Fetch event - serve from cache, fallback to network
117-
* Network-first for API calls, cache-first for static assets
117+
* Strategy:
118+
* - index.html (the navigation document) is fetched NETWORK-FIRST so the
119+
* latest cache-buster query strings in <script> / <link> tags reach the
120+
* browser on first reload after a deploy. Without this, the user has to
121+
* reload twice to see new code (the SW serves a stale index.html on the
122+
* first reload, which references the stale main.js URL).
123+
* - All other static assets are cache-first with background refresh.
124+
* - Firebase/external API calls bypass the SW entirely.
118125
*/
119126
self.addEventListener('fetch', (event) => {
120127
const url = new URL(event.request.url);
@@ -130,6 +137,26 @@ self.addEventListener('fetch', (event) => {
130137
url.hostname.includes('gstatic')) {
131138
return;
132139
}
140+
141+
// Treat navigation requests and explicit index.html requests as
142+
// network-first so post-deploy cache-busters take effect immediately.
143+
const isNavigation = event.request.mode === 'navigate'
144+
|| url.pathname === '/'
145+
|| url.pathname === '/index.html';
146+
if (isNavigation) {
147+
event.respondWith(
148+
fetch(event.request)
149+
.then((response) => {
150+
if (response && response.ok) {
151+
const clone = response.clone();
152+
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)).catch(() => {});
153+
}
154+
return response;
155+
})
156+
.catch(() => caches.match(event.request).then((cached) => cached || caches.match('/index.html')))
157+
);
158+
return;
159+
}
133160

134161
// For static assets - cache first, then network
135162
event.respondWith(

0 commit comments

Comments
 (0)