Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Incognito-old
Path: blob/main/static/src/gs/public/breaklock/service-worker.js
1333 views
1
var APP_NAME = 'breaklock',
2
APP_VERSION = 12,
3
CACHE_NAME = APP_NAME + '_' + APP_VERSION;
4
var filesToCache = [
5
'./',
6
'./?utm_source=homescreen',
7
'./app.css',
8
'./app.js',
9
'./assets/intro.svg',
10
'./assets/fonts/robotomono-light-webfont.woff2',
11
'./assets/fonts/robotomono-light-webfont.woff',
12
'./assets/fonts/robotomono-light-webfont.ttf'
13
];
14
15
// Service worker from Google Documentation
16
17
self.addEventListener('install', function(event) {
18
// Perform install steps
19
event.waitUntil(
20
caches.open(CACHE_NAME)
21
.then(function(cache) {
22
return cache.addAll(filesToCache);
23
})
24
);
25
});
26
27
self.addEventListener('activate', function(event) {
28
event.waitUntil(
29
caches.keys().then(function(cacheNames) {
30
return Promise.all(
31
cacheNames.map(function(cacheName) {
32
if (cacheName.indexOf(APP_NAME) === 0 && CACHE_NAME !== cacheName) {
33
return caches.delete(cacheName);
34
}
35
})
36
);
37
})
38
);
39
});
40
41
self.addEventListener('fetch', function(event) {
42
event.respondWith(
43
caches.match(event.request)
44
.then(function(response) {
45
// Cache hit - return response
46
if (response) {
47
return response;
48
}
49
50
// IMPORTANT: Clone the request. A request is a stream and
51
// can only be consumed once. Since we are consuming this
52
// once by cache and once by the browser for fetch, we need
53
// to clone the response.
54
var fetchRequest = event.request.clone();
55
56
return fetch(fetchRequest).then(
57
function(response) {
58
// Check if we received a valid response
59
if(!response || response.status !== 200 || response.type !== 'basic') {
60
return response;
61
}
62
63
// IMPORTANT: Clone the response. A response is a stream
64
// and because we want the browser to consume the response
65
// as well as the cache consuming the response, we need
66
// to clone it so we have two streams.
67
var responseToCache = response.clone();
68
69
caches.open(CACHE_NAME)
70
.then(function(cache) {
71
cache.put(event.request, responseToCache);
72
});
73
74
return response;
75
}
76
);
77
})
78
);
79
});
80
81