-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
94 lines (83 loc) · 2.07 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const CACHE_NAME = 'identity.simbol.io-cache-v1';
const staticCache = [
'/dist/assets/superhero.png',
'/dist/ipfs_orbitdb.js',
'/dist/polyfills.js'
]
const dynamicCache = [
'/index.html',
'/dist/index.js',
'/dist/style.css'
]
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
cache.addAll([
...staticCache,
...dynamicCache
]);
})
.then(() => {
self.skipWaiting();
})
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(() => {
caches.keys()
.then((cacheNames) => {
cacheNames = cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
});
return Promise.all(cacheNames);
})
.then(() => {
self.clients.claim();
});
});
});
/**
* Fetches request and saves it to the cache
* @param {Cache} cache - The cache where the request will be saved
* @param {Request} request - The request to fetch
* @param {Response} response - Original response if the request was already in the cache
*
* @returns {Promise} response
*/
function fetchAndAddToCache(cache, request, response) {
// Don't revalidate if it's a static file
const url = new URL(request.url)
if (response && staticCache.includes(url.pathname)) {
return response
}
const fetchPromise = fetch(request)
.then((networkResponse) => {
cache.put(request, networkResponse.clone());
return networkResponse;
});
return response || fetchPromise;
}
self.addEventListener('fetch', (event) => {
const request = event.request
const url = new URL(request.url)
if (request.method === 'GET' && (url.origin === self.location.origin || url.origin === 'https://polyfill.io')) {
event.respondWith(
// Stale-while-revalidate
caches.open(CACHE_NAME)
.then((cache) => {
return cache.match(request)
.then((response) => {
return fetchAndAddToCache(cache, request, response);
}, () => {
return fetchAndAddToCache(cache, request);
})
.catch((error) => {
console.log('log: ', error);
})
})
);
}
});