forked from MrZer0x0/trueCORD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
65 lines (58 loc) · 2.13 KB
/
Copy pathsw.js
File metadata and controls
65 lines (58 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
// trueCORD Service Worker — PWA offline support
// v2: HTML/JS всегда из сети (чтобы переводы и разметка обновлялись сразу),
// в кэш кладём только статичные ассеты (иконки). Это убирает баг с "застывшими"
// сырыми ключами i18n из-за устаревшего кэша index.php / i18n.js.
const CACHE_NAME = 'truecord-v30';
const STATIC_ASSETS = [
'./icon_tC_main.png',
];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE_NAME).then(cache =>
cache.addAll(STATIC_ASSETS).catch(() => {})
)
);
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', e => {
const url = new URL(e.request.url);
const path = url.pathname;
// Никогда не кэшируем API и загрузки
if (path.includes('truecord_api.php') || path.includes('/uploads/')) {
return;
}
// HTML-документ и динамика (index.php, i18n.js, любые .php/.js) — ТОЛЬКО из сети.
// Кэш используем лишь как аварийный фоллбэк при полном офлайне.
const isDynamic =
e.request.mode === 'navigate' ||
path.endsWith('.php') ||
path.endsWith('/') ||
path.endsWith('i18n.js') ||
path.endsWith('.js');
if (isDynamic) {
e.respondWith(
fetch(e.request, { cache: 'no-store' }).catch(() => caches.match(e.request))
);
return;
}
// Статичные ассеты (картинки, шрифты) — cache-first для скорости.
e.respondWith(
caches.match(e.request).then(hit =>
hit || fetch(e.request).then(res => {
if (res && res.status === 200 && e.request.method === 'GET') {
const clone = res.clone();
caches.open(CACHE_NAME).then(c => c.put(e.request, clone));
}
return res;
}).catch(() => hit)
)
);
});