-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
121 lines (103 loc) · 3.17 KB
/
sw.js
File metadata and controls
121 lines (103 loc) · 3.17 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
'use strict';
const CACHE_VERSION = 'v2.6';
const START_URL = '/';
const OFFLINE_URL = '/offline/';
const ASSETS = [
START_URL,
OFFLINE_URL,
'https://cdn.jsdelivr.net/gh/kaspahub/kaspahub.github.io@main/assets/fonts/mulish.woff2',
'/assets/scripts/main.js',
'/assets/styles/main.css'
];
self.addEventListener('install', function (event) {
self.skipWaiting();
const preCache = caches.open(CACHE_VERSION).then(function (cache) {
return Promise.all(
ASSETS.map(function (url) {
return cache.add(url)
.then(() => {
console.log(`[ServiceWorker] Pre-cached: ${url}`);
})
.catch(function (error) {
console.log(`[ServiceWorker] Could not save ${url}. Error: ${String(error)}`);
});
})
);
});
event.waitUntil(
preCache.then(() => {
console.log('%c[ServiceWorker] Installed successfully.', 'color: #61C554;');
})
);
});
self.addEventListener('activate', function (event) {
const cleanOldCaches = caches.keys().then(function (keys) {
return Promise.all(
keys
.filter(function (key) {
return key !== CACHE_VERSION;
})
.map(function (key) {
return caches.delete(key);
})
);
});
event.waitUntil(
cleanOldCaches.then(() => {
console.log('[ServiceWorker] Activated successfully.');
})
);
self.clients.claim();
});
self.addEventListener('fetch', function (event) {
if (!isRequestValid(event)) {
return;
}
event.respondWith(handleFetchRequest(event).catch(error => {
console.error('Fetch handler error:', error);
}));
});
function handleFetchRequest(event) {
return caches.match(event.request)
.then(function (response) {
if (response) {
// console.log(`[ServiceWorker] Loaded resource from cache: ${event.request.url}`);
return response;
}
return fetch(event.request).then(function (response) {
return caches.open(CACHE_VERSION).then(function (cache) {
cache.put(event.request, response.clone());
console.log(`[ServiceWorker] Downloaded and cached: ${event.request.url}`);
return response;
});
});
})
.catch(function () {
console.log(`[ServiceWorker] Could not load: ${event.request.url}. Serving offline fallback.`);
return caches.match(OFFLINE_URL);
});
}
function isRequestValid(event) {
const url = new URL(event.request.url);
const isSameOrigin = url.origin === self.location.origin;
const isSecure = url.protocol.startsWith('https');
const isGet = event.request.method === 'GET';
// const isCacheablePath = ASSETS.includes(url.pathname);
if (!isSameOrigin) {
console.log(`[ServiceWorker] Skipping request: different origin → ${url.origin}`);
return false;
}
if (!isSecure) {
console.log(`[ServiceWorker] Skipping request: insecure protocol → ${url.protocol}`);
return false;
}
if (!isGet) {
console.log(`[ServiceWorker] Skipping request: non-GET method → ${event.request.method} → ${event.request.url}`);
return false;
}
// if (!isCacheablePath) {
// console.log(`[ServiceWorker] Skipping request: not in cacheable paths → ${url.pathname}`);
// return false;
// }
return true;
}