Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
69 lines (63 loc) · 2.13 KB
/
Copy pathsw.js
File metadata and controls
69 lines (63 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const CACHE_NAME = 'iron-v4-volt';
const STATIC_ASSETS = [
'/data/exercises.js',
'/data/foods.js',
'/data/programs.js',
'/data/references.js',
'/data/recipes.js',
'/js/calculators.js',
'/manifest.json'
];
// Install: cache only truly static data files (not index.html)
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
);
self.skipWaiting();
});
// Activate: wipe all old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))
)
);
self.clients.claim();
});
// Fetch strategy:
// index.html → network-first (always get latest, fall back to cache)
// data/*.js → cache-first (static data, safe to cache long-term)
// everything else → network-first
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
const isHtml = url.pathname.endsWith('.html') || url.pathname === '/' || url.pathname.endsWith('/muscleforge/');
const isData = url.pathname.includes('/data/') || url.pathname.includes('/js/') || url.pathname.endsWith('/manifest.json');
if (url.origin !== self.location.origin) {
// External (CDN, fonts) — network-first, cache as fallback
event.respondWith(
fetch(event.request).catch(() => caches.match(event.request))
);
return;
}
if (isData) {
// Cache-first for static data files
event.respondWith(
caches.match(event.request).then((cached) =>
cached || fetch(event.request).then((response) => {
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, response.clone()));
return response;
})
)
);
return;
}
// Network-first for HTML and everything else (ensures updates are always seen)
event.respondWith(
fetch(event.request).then((response) => {
if (isHtml) {
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, response.clone()));
}
return response;
}).catch(() => caches.match(event.request))
);
});