forked from lgu-ms/lgu-ms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
executable file
·35 lines (32 loc) · 1.07 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
const cacheName = 'lgu';
const domain = "http://localhost/lgu";
self.addEventListener('activate', event => {
event.waitUntil(
(async () => {
const keys = await caches.keys();
return keys.map(async (cache) => {
if(cache !== cacheName) {
console.log('Service Worker: Removing old cache: '+ cache);
return await caches.delete(cache);
}
})
})()
)
})
self.addEventListener('install', (event) => {
event.waitUntil(caches.open(cacheName));
});
self.addEventListener('fetch', async (event) => {
if (event.request.method !== "GET") return;
if ((event.request.destination === 'image' || event.request.url.includes("/assets/") && event.request.url.includes(domain))) {
event.respondWith(caches.open(cacheName).then((cache) => {
return cache.match(event.request).then((cachedResponse) => {
return cachedResponse || fetch(event.request.url).then((fetchedResponse) => {
cache.put(event.request, fetchedResponse.clone());
return fetchedResponse;
});
});
}));
}
return;
});