Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
68 lines (62 loc) · 2.15 KB
/
Copy pathsw.js
File metadata and controls
68 lines (62 loc) · 2.15 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
/**
* Service Worker for omerdev (bruhgit.github.io)
* Enables 100% offline support, instant caching, and PWA installation.
*/
const CACHE_NAME = 'omerdev-pwa-v1.0';
const STATIC_ASSETS = [
'./',
'./index.html',
'./styles.css?v=2.4',
'./app.js?v=2.2',
'./config.js?v=2.2',
'./site.webmanifest',
'./robots.txt',
'./humans.txt',
'./security.txt',
'./browserconfig.xml',
'https://github.com/bruhgit.png'
];
// Install Event - Pre-cache core assets
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
return cache.addAll(STATIC_ASSETS).catch(err => {
console.warn('PWA Pre-cache item failed:', err);
});
}).then(() => self.skipWaiting())
);
});
// Activate Event - Clean up old cache versions
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys => {
return Promise.all(
keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key))
);
}).then(() => self.clients.claim())
);
});
// Fetch Event - Stale-while-revalidate strategy for maximum speed & offline access
self.addEventListener('fetch', event => {
// Only handle GET requests and skip GitHub API requests from aggressive offline caching
if (event.request.method !== 'GET' || event.request.url.includes('api.github.com')) {
return;
}
event.respondWith(
caches.match(event.request).then(cachedResponse => {
const fetchPromise = fetch(event.request).then(networkResponse => {
if (networkResponse && networkResponse.status === 200) {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, responseClone);
});
}
return networkResponse;
}).catch(() => {
// If offline and not in cache, fallback
return cachedResponse;
});
return cachedResponse || fetchPromise;
})
);
});