-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.js.dist
58 lines (52 loc) · 1.39 KB
/
sw.js.dist
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
/** SW auto-generated on %APP_BUILD_DATE% **/
const CACHE_KEY = 'im-%APP_VERSION%';
const assets = [
%APP_ASSETS%
];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_KEY)
.then(function(cache) {
return cache.addAll(assets);
})
);
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
return CACHE_KEY !== cacheName;
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('fetch', function(event) {
const requestUrl = new URL(
event.request.url
);
if (requestUrl.pathname.includes("assets/") && requestUrl.hostname === self.location.hostname) {
const promiseResponse = caches.open(CACHE_KEY)
.then(function(cache) {
return cache.match(event.request)
.then(function(response) {
if (response) {
return response;
} else {
return fetch(event.request)
.then(function(response) {
cache.put(
event.request,
response.clone()
);
return response;
});
}
});
});
event.respondWith(promiseResponse);
}
});