-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathservice-worker.js
More file actions
36 lines (31 loc) · 945 Bytes
/
service-worker.js
File metadata and controls
36 lines (31 loc) · 945 Bytes
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
const CACHE_NAME = "wtfjht-lite-cache-v3";
const urlsToCache = [
"https://whatthefuckjusthappenedtoday.com/web-app-manifest-192x192.png?v=3",
"https://whatthefuckjusthappenedtoday.com/web-app-manifest-512x512.png?v=3"
];
self.addEventListener("install", event => {
self.skipWaiting();
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
// Cache each URL individually, ignoring failures (e.g., in local dev)
return Promise.all(
urlsToCache.map(url =>
cache.add(url).catch(() => {})
)
);
})
);
});
self.addEventListener("activate", event => {
clients.claim();
});
self.addEventListener("fetch", event => {
const requestUrl = event.request.url;
if (urlsToCache.some(url => requestUrl.includes(new URL(url).pathname))) {
event.respondWith(
caches.match(event.request).then(response => {
return response || fetch(event.request);
})
);
}
});