-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
68 lines (64 loc) · 1.98 KB
/
sw.js
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
const CACHE_NAME = 'jws-pictures-cache-v1';
const urlsToCache = [
'/',
'/index.html',
'/main.js',
'https://jws.social/fileserver/01E5SQ87G8HX9T4NYTPR147MJF/attachment/original/01HYRS8T6WMW6VMSHW8MKT2HC0.png',
'/https://jws.pictures/photos/all.json',
'https://jws.pictures/photos/2024.json',
'https://jws.pictures/photos/2023.json',
'https://jws.pictures/photos/2022.json',
'https://jws.pictures/photos/2021.json',
'https://jws.pictures/photos/2020.json',
'https://jws.pictures/photos/2019.json',
'https://jws.pictures/photos/2018.json',
];
// Install the service worker and cache the app shell
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('Opened cache:', CACHE_NAME);
return cache.addAll(urlsToCache);
})
);
});
// Intercept fetch requests and serve cached assets if available
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
// Return the cached version or fetch from network
return response || fetch(event.request);
})
);
});
// Activate the service worker and remove old caches
self.addEventListener('activate', event => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (!cacheWhitelist.includes(cacheName)) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
});
// Register the service worker
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
.then(registration => {
console.log('ServiceWorker registered with scope:', registration.scope);
})
.catch(error => {
console.log('ServiceWorker registration failed:', error);
});
});
}