Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
112 lines (102 loc) · 3.14 KB
/
Copy pathsw.js
File metadata and controls
112 lines (102 loc) · 3.14 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* PROJECT 417 // SERVICE WORKER (OFFLINE FIRST PWA ENGINE)
* Caches core app shell, static assets, and enables offline training in basement gyms.
* Strict Zero-Emoji compliance.
*/
const CACHE_NAME = 'project-417-v2.1';
const CORE_PRECACHE_URLS = [
'/',
'/index.html',
'/manifest.json',
'/favicon.svg',
'/favicon-32x32.png',
'/favicon-16x16.png',
'/logo-417.svg',
'/apple-touch-icon.png',
'/telegram-web-app.js',
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(CORE_PRECACHE_URLS).catch(() => {
// Continue even if some individual icons fail
});
}).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((name) => {
if (name !== CACHE_NAME) {
return caches.delete(name);
}
return Promise.resolve();
})
);
}).then(() => self.clients.claim())
);
});
self.addEventListener('fetch', (event) => {
const request = event.request;
if (request.method !== 'GET') return;
const url = new URL(request.url);
// Skip API or Telegram external calls
if (url.pathname.startsWith('/api/') || url.hostname.includes('telegram.org')) {
return;
}
// Navigation requests: Network first, fall back to cached index.html
if (request.mode === 'navigate') {
event.respondWith(
fetch(request)
.then((response) => {
if (response && response.status === 200) {
const copy = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
}
return response;
})
.catch(async () => {
const cached = await caches.match('/index.html');
return cached || (await caches.match('/'));
})
);
return;
}
// Static Assets (JS, CSS, SVGs, Fonts, Images): Cache First with background update
if (
url.pathname.startsWith('/assets/') ||
url.pathname.endsWith('.js') ||
url.pathname.endsWith('.css') ||
url.pathname.endsWith('.svg') ||
url.pathname.endsWith('.png') ||
url.pathname.endsWith('.woff2')
) {
event.respondWith(
caches.match(request).then((cachedResponse) => {
if (cachedResponse) {
// Fetch update in background
fetch(request).then((networkResponse) => {
if (networkResponse && networkResponse.status === 200) {
caches.open(CACHE_NAME).then((cache) => cache.put(request, networkResponse));
}
}).catch(() => {});
return cachedResponse;
}
return fetch(request).then((networkResponse) => {
if (networkResponse && networkResponse.status === 200) {
const copy = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
}
return networkResponse;
});
})
);
return;
}
// Default fallback
event.respondWith(
caches.match(request).then((cached) => cached || fetch(request))
);
});