-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
398 lines (365 loc) · 11.8 KB
/
sw.js
File metadata and controls
398 lines (365 loc) · 11.8 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
const CACHE_VERSION = 'v0.2.0-1e884eeb';
const CACHE_NAME = `bygog-lab-cache-${CACHE_VERSION}`;
const OFFLINE_URL = 'index.html';
// Core assets for app shell
const urlsToCache = [
'.',
'index.html',
'manifest.json',
'dist/styles.0aa70435.css',
'dist/fab.7a4a0da4.css',
'dist/renderLinks.1938d1d0.js',
'data/links-index.json',
'icon/bygog-lab-icon.svg',
'icon/bygog-lab-logo.svg'
];
// Pre-cache a curated set of very small icons to improve first paint
// Keep this list limited to tiny SVGs (<5KB) to avoid bloating cache
const smallIcons = [
'icon/fallback.svg',
'icon/amd.svg',
'icon/android.svg',
'icon/buster.svg',
'icon/chrome.svg',
'icon/discord.svg',
'icon/github.svg',
'icon/gmail.svg',
'icon/googledrive.svg',
'icon/rustdesk.svg',
'icon/steam.svg',
'icon/teamviewer.svg',
'icon/telegram.svg',
'icon/tor.svg',
'icon/ubuntu.svg',
'icon/ublock.svg',
'icon/visualstudiocode.svg',
'icon/winconfigs.svg',
'icon/yahoo.svg',
'icon/zenbrowser.svg'
];
self.addEventListener('install', event => {
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE_NAME);
// Cache core assets
try {
await cache.addAll([...urlsToCache, ...smallIcons]);
} catch (err) {
console.warn('Cache addAll failed:', err);
}
// Don't skipWaiting here — let the client decide via SKIP_WAITING message
// so the update banner can be shown to the user.
})()
);
});
self.addEventListener('activate', event => {
event.waitUntil(
(async () => {
// Clean up old caches
const keys = await caches.keys();
await Promise.all(
keys.map(k => (k !== CACHE_NAME ? caches.delete(k) : undefined))
);
// Enable navigation preload if supported
try {
if (self.registration?.navigationPreload) {
await self.registration.navigationPreload.enable();
}
} catch {}
// Take control of all clients
await self.clients.claim();
})()
);
});
// Handle messages from clients
self.addEventListener('message', event => {
try {
const data = event.data || {};
if (data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
if (data.type === 'GET_VERSION') {
event.ports[0]?.postMessage({ version: CACHE_VERSION });
}
if (data.type === 'CACHE_URLS' && Array.isArray(data.urls)) {
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE_NAME);
const unique = [...new Set(data.urls)];
await cache.addAll(unique);
})()
);
}
} catch {}
});
// Periodic background sync for content updates
self.addEventListener('periodicsync', event => {
if (event.tag === 'update-content') {
event.waitUntil(
(async () => {
try {
const cache = await caches.open(CACHE_NAME);
// Refresh critical assets
const refreshUrls = ['data/links-index.json', 'index.html'];
for (const url of refreshUrls) {
try {
const response = await fetch(url, { cache: 'no-store' });
if (response.ok) {
await cache.put(url, response);
}
} catch {}
}
} catch {}
})()
);
}
});
// Push notification support
self.addEventListener('push', event => {
if (!event.data) return;
try {
const data = event.data.json();
const options = {
body: data.body || 'Yeni güncelleme mevcut!',
icon: 'icon/bygog-lab-icon-192.png',
badge: 'icon/bygog-lab-icon-192.png',
vibrate: [100, 50, 100],
data: {
url: data.url || './',
dateOfArrival: Date.now()
},
actions: [
{ action: 'open', title: 'Aç' },
{ action: 'close', title: 'Kapat' }
],
tag: data.tag || 'bygog-notification',
renotify: true
};
event.waitUntil(
self.registration.showNotification(data.title || 'byGOG Lab', options)
);
} catch {}
});
// Notification click handler
self.addEventListener('notificationclick', event => {
event.notification.close();
if (event.action === 'close') return;
const urlToOpen = event.notification.data?.url || './';
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(windowClients => {
// Focus existing window if available
for (const client of windowClients) {
if (client.url.includes('byGOG-Lab') && 'focus' in client) {
return client.focus();
}
}
// Open new window
if (clients.openWindow) {
return clients.openWindow(urlToOpen);
}
})
);
});
// Background fetch for large downloads
self.addEventListener('backgroundfetchsuccess', event => {
event.waitUntil(
(async () => {
try {
const cache = await caches.open(CACHE_NAME);
const records = await event.registration.matchAll();
for (const record of records) {
const response = await record.responseReady;
await cache.put(record.request, response);
}
await event.updateUI({ title: 'İndirme tamamlandı!' });
} catch {}
})()
);
});
self.addEventListener('backgroundfetchfail', event => {
console.warn('Background fetch failed:', event.registration.id);
});
self.addEventListener('backgroundfetchabort', event => {
console.warn('Background fetch aborted:', event.registration.id);
});
// Advanced fetch handler with modern caching strategies
self.addEventListener('fetch', event => {
const req = event.request;
if (req.method !== 'GET') return;
event.respondWith((async () => {
const cache = await caches.open(CACHE_NAME);
const url = new URL(req.url);
const sameOrigin = url.origin === self.location.origin;
const isHTML = req.mode === 'navigate' || (req.headers.get('accept') || '').includes('text/html');
const isStatic = sameOrigin && /\.(?:css|js|svg|png|jpg|jpeg|webp|ico|json|woff2?)$/.test(url.pathname);
const isHashed = sameOrigin && /\.[0-9a-f]{8,}\.(?:css|js)$/.test(url.pathname);
const isImage = /\.(?:png|jpg|jpeg|webp|svg|gif|ico)$/i.test(url.pathname);
const isFont = /\.(?:woff2?|ttf|eot|otf)$/i.test(url.pathname);
const isJson = sameOrigin && /\.json$/i.test(url.pathname);
// Network-first for HTML navigations (for fresh content)
if (isHTML) {
try {
// Use navigation preload if available
const pre = await (event.preloadResponse || Promise.resolve(undefined));
const net = pre || await fetch(req, {
credentials: 'same-origin',
headers: { 'Cache-Control': 'no-cache' }
});
if (net && net.status === 200) {
try { await cache.put(req, net.clone()); } catch {}
}
return net;
} catch {
const cached = await cache.match(req) || await cache.match(OFFLINE_URL);
return cached || createOfflineResponse();
}
}
// Stale-while-revalidate for JSON data files — notify client if content changed
if (isJson) {
const cached = await cache.match(req);
const fetchPromise = (async () => {
try {
const net = await fetch(req, { cache: 'no-store' });
if (net && net.status === 200) {
// Check if content actually changed
if (cached) {
try {
const [oldText, newText] = await Promise.all([
cached.clone().text(),
net.clone().text()
]);
if (oldText !== newText) {
// Notify all clients about data change
const allClients = await self.clients.matchAll({ type: 'window' });
for (const client of allClients) {
client.postMessage({ type: 'DATA_UPDATED', url: req.url });
}
}
} catch {}
}
await cache.put(req, net.clone());
return net;
}
} catch {}
return null;
})();
if (cached) {
event.waitUntil(fetchPromise);
return cached;
}
const net = await fetchPromise;
return net || createOfflineResponse();
}
// Cache-first for hashed/immutable assets (forever cacheable)
if (isStatic && isHashed) {
const cached = await cache.match(req);
if (cached) return cached;
try {
const net = await fetch(req);
if (net && net.status === 200) {
try { await cache.put(req, net.clone()); } catch {}
}
return net;
} catch {
return createOfflineResponse();
}
}
// Cache-first for fonts (rarely change)
if (isFont) {
const cached = await cache.match(req);
if (cached) return cached;
try {
const net = await fetch(req);
if (net && net.status === 200) {
try { await cache.put(req, net.clone()); } catch {}
}
return net;
} catch {
return createOfflineResponse();
}
}
// Stale-while-revalidate for images
if (isImage) {
const cached = await cache.match(req);
const fetchPromise = fetch(req).then(net => {
if (net && net.status === 200) {
cache.put(req, net.clone()).catch(() => {});
}
return net;
}).catch(() => null);
if (cached) {
event.waitUntil(fetchPromise);
return cached;
}
const net = await fetchPromise;
return net || createOfflineResponse();
}
// Stale-while-revalidate for other same-origin static assets
if (isStatic) {
const cached = await cache.match(req);
if (cached) {
event.waitUntil((async () => {
try {
const net = await fetch(req);
if (net && net.status === 200) await cache.put(req, net.clone());
} catch {}
})());
return cached;
}
try {
const net = await fetch(req);
if (net && net.status === 200) await cache.put(req, net.clone());
return net;
} catch {
return createOfflineResponse();
}
}
// Default: Network with cache fallback
try {
const net = await fetch(req);
if (sameOrigin && net && net.status === 200) {
try { await cache.put(req, net.clone()); } catch {}
}
return net;
} catch {
const cached = await cache.match(req);
return cached || createOfflineResponse();
}
})());
});
// Create offline response
function createOfflineResponse() {
return new Response(
`<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Çevrimdışı - byGOG Lab</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:system-ui,-apple-system,sans-serif;background:#181c22;color:#e5e7eb;min-height:100vh;display:flex;align-items:center;justify-content:center;text-align:center;padding:20px}
.offline{max-width:400px}
.offline-icon{font-size:64px;margin-bottom:20px}
h1{font-size:24px;margin-bottom:12px;color:#a3bffa}
p{color:#9ca3af;margin-bottom:20px;line-height:1.6}
button{background:#3b82f6;color:#fff;border:none;padding:12px 24px;border-radius:8px;font-size:16px;cursor:pointer;transition:background 0.2s}
button:hover{background:#2563eb}
</style>
</head>
<body>
<div class="offline">
<div class="offline-icon">📡</div>
<h1>Çevrimdışısınız</h1>
<p>İnternet bağlantınız yok gibi görünüyor. Lütfen bağlantınızı kontrol edin ve tekrar deneyin.</p>
<button onclick="location.reload()">Tekrar Dene</button>
</div>
</body>
</html>`,
{
status: 503,
statusText: 'Offline',
headers: { 'Content-Type': 'text/html; charset=utf-8' }
}
);
}