-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__service-worker.js
More file actions
50 lines (44 loc) · 1.42 KB
/
__service-worker.js
File metadata and controls
50 lines (44 loc) · 1.42 KB
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
//--------------------------------------------------------------------------
// You can find dozens of practical, detailed, and working examples of
// service worker usage on https://github.com/mozilla/serviceworker-cookbook
//--------------------------------------------------------------------------
// Version
var VERSION = '2.2.1'
// Cache name
var CACHE_NAME = 'cache-version-' + VERSION;
// Files
var REQUIRED_FILES = [
'index.html'
];
self.addEventListener('install', function (event) {
// Perform install step: loading each required file into cache
event.waitUntil(
caches.open(CACHE_NAME)
.then(function (cache) {
// Add all offline dependencies to the cache
return cache.addAll(REQUIRED_FILES);
})
.then(function () {
return self.skipWaiting();
})
);
});
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.match(event.request)
.then(function (response) {
// Cache hit - return the response from the cached version
if (response) {
return response;
}
// Not in cache - return the result from the live server
// `fetch` is essentially a "fallback"
return fetch(event.request);
}
)
);
});
self.addEventListener('activate', function (event) {
// Calling claim() to force a "controllerchange" event on navigator.serviceWorker
event.waitUntil(self.clients.claim());
});