-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_service_worker.html
More file actions
59 lines (48 loc) · 1.47 KB
/
test_service_worker.html
File metadata and controls
59 lines (48 loc) · 1.47 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
51
52
53
54
55
56
57
58
59
<script type="text/javascript">
var filesToCache = [
'.',
'style/main.css',
'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700',
'images/still_life-1600_large_2x.jpg',
'images/still_life-800_large_1x.jpg',
'images/still_life_small.jpg',
'images/still_life_medium.jpg',
'index.html',
'pages/offline.html',
'pages/404.html'
];
var staticCacheName = 'pages-cache-v1';
self.addEventListener('install', function(event) {
console.log('Attempting to install service worker and cache static assets');
event.waitUntil(
caches.open(staticCacheName)
.then(function(cache) {
return cache.addAll(filesToCache);
})
);
});
self.addEventListener('fetch', function(event) {
console.log('Fetch event for ', event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
console.log('Found ', event.request.url, ' in cache');
return response;
}
console.log('Network request for ', event.request.url);
return fetch(event.request)
.then(function(response) {
// TODO 5 - Respond with custom 404 page
return caches.open(staticCacheName).then(function(cache) {
if (event.request.url.indexOf('test') < 0) {
cache.put(event.request.url, response.clone());
}
return response;
});
});
}).catch(function(error) {
// TODO 6 - Respond with custom offline page
})
);
});
</script>