Skip to content

Commit 0381af3

Browse files
authored
Create service-worker.js
1 parent f6837b7 commit 0381af3

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

service-worker.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const CACHE_NAME = 'smp-cache-v1';
2+
const urlsToCache = [
3+
'/',
4+
'/index.html',
5+
'/style.css',
6+
'/favicon.ico',
7+
'/icon-192x192.png',
8+
'/icon-512x512.png',
9+
];
10+
11+
self.addEventListener('install', (event) => {
12+
event.waitUntil(
13+
caches.open(CACHE_NAME).then((cache) => {
14+
return cache.addAll(urlsToCache);
15+
})
16+
);
17+
});
18+
19+
self.addEventListener('fetch', (event) => {
20+
event.respondWith(
21+
caches.match(event.request).then((cachedResponse) => {
22+
return cachedResponse || fetch(event.request);
23+
})
24+
);
25+
});
26+
27+
self.addEventListener('activate', (event) => {
28+
const cacheWhitelist = [CACHE_NAME];
29+
event.waitUntil(
30+
caches.keys().then((cacheNames) => {
31+
return Promise.all(
32+
cacheNames.map((cacheName) => {
33+
if (!cacheWhitelist.includes(cacheName)) {
34+
return caches.delete(cacheName);
35+
}
36+
})
37+
);
38+
})
39+
);
40+
});

0 commit comments

Comments
 (0)