-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
48 lines (45 loc) · 1.56 KB
/
sw.js
File metadata and controls
48 lines (45 loc) · 1.56 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
// Terminator2 Service Worker — caches the background video and static assets
var CACHE_NAME = 't2-v2';
var PRECACHE = ['/eyesblink.mp4'];
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(PRECACHE);
}).then(function() {
return self.skipWaiting();
})
);
});
self.addEventListener('activate', function(e) {
e.waitUntil(
caches.keys().then(function(names) {
return Promise.all(
names.filter(function(n) { return n !== CACHE_NAME; })
.map(function(n) { return caches.delete(n); })
);
}).then(function() {
return self.clients.claim();
})
);
});
self.addEventListener('fetch', function(e) {
var url = new URL(e.request.url);
// Only cache-first for the video and static assets on same origin
if (url.origin !== location.origin) return;
// Cache-first for the video file
if (url.pathname === '/eyesblink.mp4') {
e.respondWith(
caches.match(e.request).then(function(cached) {
return cached || fetch(e.request).then(function(resp) {
var clone = resp.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(e.request, clone);
});
return resp;
});
})
);
return;
}
// Network-first for everything else (HTML, CSS, JS, JSON change frequently)
});