-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservice-worker.js
More file actions
124 lines (113 loc) · 3.96 KB
/
service-worker.js
File metadata and controls
124 lines (113 loc) · 3.96 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
const CACHE_NAME = 'cast-volume-control-v2';
const urlsToCache = [
'/',
'/index.html',
'/manifest.json',
'/favicons/android-chrome-192x192.png',
'/favicons/android-chrome-512x512.png',
'/favicons/apple-touch-icon.png',
'/favicons/favicon-16x16.png',
'/favicons/favicon-32x32.png',
'/favicons/favicon.ico',
'https://cdn.tailwindcss.com',
'https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1',
'https://buttons.github.io/buttons.js'
];
self.addEventListener('install', (event) => {
console.log('Service Worker installing...');
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Opened cache');
return cache.addAll(urlsToCache.map(url => new Request(url, {mode: 'no-cors'})));
})
.catch((error) => {
console.error('Cache installation failed:', error);
return caches.open(CACHE_NAME).then(cache => {
const essentialUrls = ['/', '/index.html', '/manifest.json'];
return cache.addAll(essentialUrls);
});
})
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
console.log('Service Worker activated');
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}).then(() => {
return self.clients.claim();
})
);
});
self.addEventListener('fetch', (event) => {
if (event.request.url.includes('chrome-extension') ||
event.request.url.includes('moz-extension') ||
event.request.method !== 'GET') {
return;
}
event.respondWith(
caches.match(event.request)
.then((response) => {
if (response) {
return response;
}
return fetch(event.request.clone())
.then((response) => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
})
.catch((error) => {
console.error('Fetch failed:', error);
if (event.request.destination === 'document') {
return caches.match('/index.html');
}
throw error;
});
})
);
});
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
self.addEventListener('sync', (event) => {
if (event.tag === 'background-sync') {
console.log('Background sync triggered');
event.waitUntil(doBackgroundSync());
}
});
async function doBackgroundSync() {
try {
const cache = await caches.open(CACHE_NAME);
const essentialUrls = ['/', '/index.html'];
for (const url of essentialUrls) {
try {
const response = await fetch(url);
if (response.ok) {
await cache.put(url, response);
}
} catch (error) {
console.error('Background sync failed for:', url, error);
}
}
} catch (error) {
console.error('Background sync failed:', error);
}
}