-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
68 lines (61 loc) · 2.08 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
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
(function () {
"use strict";
var cacheNameStatic = 'cloudinary-pwa-react-v1';
var currentCacheNames = [ cacheNameStatic ];
var cachedUrls = [
// 3rd party CDN
'https://unpkg.com/babel-core@5.8.38/browser.min.js',
'https://unpkg.com/lodash@4.17.4/lodash.js',
'https://unpkg.com/react@15.3.1/dist/react.min.js',
'https://unpkg.com/react-dom@15.3.1/dist/react-dom.min.js',
'https://unpkg.com/react-router-dom/umd/react-router-dom.min.js',
'https://unpkg.com/cloudinary-core@2.3.0/cloudinary-core.js',
'https://unpkg.com/cloudinary-react@1.0.3/dist/cloudinary-react.js',
'https://unpkg.com/axios/dist/axios.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css',
// Local assets
'/style.css',
// Fake API
'/images.json'
];
// A new ServiceWorker has been registered
self.addEventListener("install", function (event) {
event.waitUntil(
caches.delete(cacheNameStatic).then(function() {
return caches.open(cacheNameStatic);
}).then(function (cache) {
return cache.addAll(cachedUrls);
}).catch(function(e) {
})
);
});
// A new ServiceWorker is now active
self.addEventListener("activate", function (event) {
event.waitUntil(
caches.keys()
.then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (currentCacheNames.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
// Save thing to cache in process of use
self.addEventListener("fetch", function (event) {
event.respondWith(
caches.open(cacheNameStatic).then(function(cache) {
return cache.match(event.request).then(function(response) {
var fetchPromise = fetch(event.request).then(function(networkResponse) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
})
return response || fetchPromise;
})
})
);
});
})();