-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
81 lines (73 loc) · 2.62 KB
/
Copy pathsw.js
File metadata and controls
81 lines (73 loc) · 2.62 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
// GameHub Service Worker
// Cache-first untuk asset statis, network-first untuk HTML
const CACHE_VERSION = 'gamehub-v19-stickman';
const STATIC_CACHE = `${CACHE_VERSION}-static`;
const RUNTIME_CACHE = `${CACHE_VERSION}-runtime`;
// Derive scope base path dari lokasi sw.js sendiri
// Bekerja baik di GitHub Pages /File-GameHub/ maupun root /
const SCOPE = self.location.pathname.replace(/sw\.js$/, '');
const STATIC_ASSETS = [
SCOPE,
SCOPE + 'index.html',
SCOPE + 'manifest.json',
SCOPE + 'icon.svg',
SCOPE + 'icons/icon-192.png',
SCOPE + 'icons/icon-512.png',
];
// Listen untuk skipWaiting message dari client
self.addEventListener('message', event => {
if (event.data && event.data.action === 'skipWaiting') self.skipWaiting();
});
// INSTALL: pre-cache static assets (per-file, jangan all-or-nothing)
self.addEventListener('install', event => {
event.waitUntil(
caches.open(STATIC_CACHE).then(cache =>
// Pakai Promise.allSettled supaya 1 file gagal tidak blokir SW install
Promise.allSettled(
STATIC_ASSETS.map(url => cache.add(url).catch(err => console.warn('SW pre-cache miss:', url, err)))
)
).then(() => self.skipWaiting())
);
});
// ACTIVATE: clean up old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys =>
Promise.all(
keys.filter(k => !k.startsWith(CACHE_VERSION)).map(k => caches.delete(k))
)
).then(() => self.clients.claim())
);
});
// FETCH: serve from cache if available, fallback to network
self.addEventListener('fetch', event => {
const { request } = event;
if (request.method !== 'GET') return;
// Skip cross-origin (CDN, dll - termasuk EmulatorJS CDN)
const url = new URL(request.url);
if (url.origin !== location.origin) return;
// Network-first untuk HTML (selalu coba dapat versi terbaru)
if (request.mode === 'navigate' || request.headers.get('accept')?.includes('text/html')) {
event.respondWith(
fetch(request)
.then(response => {
const copy = response.clone();
caches.open(RUNTIME_CACHE).then(cache => cache.put(request, copy));
return response;
})
.catch(() => caches.match(request).then(r => r || caches.match(SCOPE + 'index.html')))
);
return;
}
// Cache-first untuk asset (PNG, CSS, JS)
event.respondWith(
caches.match(request).then(cached => {
if (cached) return cached;
return fetch(request).then(response => {
const copy = response.clone();
caches.open(RUNTIME_CACHE).then(cache => cache.put(request, copy));
return response;
}).catch(() => cached);
})
);
});