-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceWorker.js
72 lines (62 loc) · 1.66 KB
/
serviceWorker.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
69
70
71
72
// self.skipWaiting();
// const version = 1;
// const appCache = caches.open(`cahce_${version}`);
const staticCoffees = 'coffees';
const assets = [
"/",
"/manifest.json",
"/index.html",
"/styles.css",
"/app.js",
"/images/coffee-1.jpg",
"/images/coffee-2.jpg",
"/images/coffee-3.jpg",
"/images/coffee-4.jpg",
"/images/coffee-5.jpg",
"/images/coffee-6.jpg",
"/images/coffee-7.jpg",
"/images/coffee-8.jpg",
"/images/coffee-9.jpg",
"/images/icon-192.png",
];
self.addEventListener('install', (event) => {
console.log('Install event', event);
event.waitUntil(
caches.open(staticCoffees).then((cache) => cache.addAll(assets))
// staticCoffees.open(staticCoffees).then((cache) => cache.addAll(assets))
);
});
self.addEventListener('activate', (event) => {
console.log(event);
});
self.addEventListener('fetch', (event) => {
{/* Cache only */}
// event.respondWith(
// appCache.match(event.request).then((response) => {
// return response;
// })
// );
// Network only
// event.respondWith(
// appCache.match(event.request).then((response) => {
// return fetch(event.request);
// })
// );
// Cache first, falling back to network
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request);
})
);
});
self.addEventListener('push', event => {
const payload = event.data.json();
const options = {
body: payload.body,
icon: '/images/icon-192.jpg', // You can customize the notification icon
vibrate: [200, 100, 200], // Vibration pattern
};
event.waitUntil(
self.registration.showNotification(payload.title, options)
);
});