-
Notifications
You must be signed in to change notification settings - Fork 6
/
service-worker.js
92 lines (85 loc) · 2.76 KB
/
service-worker.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Cache version and files to cache
const CACHE_NAME = 'pwa-cache-v1';
const urlsToCache = [
'/style.css',
'/manifest.json',
'/',
];
// Install event - cache the specified resources
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll(urlsToCache);
})
);
// Take control of the page immediately after installation
self.skipWaiting();
});
// Fetch event - use network-first strategy
self.addEventListener('fetch', event => {
event.respondWith(
fetch(event.request)
.then(response => {
// If the network request is successful, update the cache with the fresh response
return caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, response.clone()); // Cache the latest version
return response; // Return the fresh response to the user
});
})
.catch(() => {
// If the network request fails, fall back to the cache
return caches.match(event.request);
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', event => {
const cacheWhitelist = [CACHE_NAME]; // Only keep the latest cache
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName); // Delete outdated caches
}
})
);
})
);
// Ensure the service worker takes control immediately
return self.clients.claim();
});
// Push notification event listener
self.addEventListener('push', event => {
const data = event.data ? event.data.json() : {};
const title = data.title || 'New Notification';
const options = {
body: data.body || 'You have a new message!',
icon: data.icon || '/icon.png',
data: {
url: data.url || '/'
}
};
event.waitUntil(
self.registration.showNotification(title, options)
);
});
// Notification click event listener
self.addEventListener('notificationclick', event => {
event.notification.close();
const urlToOpen = event.notification.data.url;
event.waitUntil(
clients.matchAll({
type: 'window',
includeUncontrolled: true
}).then(clientList => {
const client = clientList.find(c => c.url === urlToOpen && 'focus' in c);
if (client) {
return client.focus();
} else if (clients.openWindow) {
return clients.openWindow(urlToOpen);
}
})
);
});