-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathworker.js
59 lines (55 loc) · 1.95 KB
/
worker.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
/* global self, caches, fetch */
'use strict'
var cachename = 'shopping-list-vuejs-pouchdb-0.0.1'
var urlstocache = [
'index.html',
'shoppinglist.js',
'shoppinglist.css',
'https://cdnjs.cloudflare.com/ajax/libs/cuid/1.3.8/browser-cuid.min.js',
'https://cdn.jsdelivr.net/gh/pouchdb/[email protected]/dist/pouchdb.min.js',
'https://cdn.jsdelivr.net/gh/pouchdb/[email protected]/dist/pouchdb.find.min.js',
'https://unpkg.com/[email protected]/dist/vue-material.js',
'https://unpkg.com/[email protected]/dist/vue.js',
'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic',
'https://fonts.googleapis.com/icon?family=Material+Icons',
'https://unpkg.com/[email protected]/dist/vue-material.css',
'https://fonts.gstatic.com/s/roboto/v16/oMMgfZMQthOryQo9n22dcuvvDin1pK8aKteLpeZ5c0A.woff2',
'https://fonts.gstatic.com/s/roboto/v16/RxZJdnzeo3R5zSexge8UUZBw1xU1rKptJj_0jans920.woff2',
'https://fonts.gstatic.com/s/materialicons/v29/2fcrYFNaTjcS6g4U3t-Y5UEw0lE80llgEseQY3FEmqw.woff2'
];
// install/cache page assets
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(cachename)
.then(function (cache) {
console.log('cache opened')
return cache.addAll(urlstocache)
})
)
})
// intercept page requests
self.addEventListener('fetch', function (event) {
console.log(event.request.url)
event.respondWith(
caches.match(event.request).then(function (response) {
// serve requests from cache (if found)
return response || fetch(event.request)
})
)
})
// service worker activated, remove outdated cache
self.addEventListener('activate', function (event) {
console.log('worker activated')
event.waitUntil(
caches.keys().then(function (keys) {
return Promise.all(
keys.filter(function (key) {
// filter old versioned keys
return key !== cachename
}).map(function (key) {
return caches.delete(key)
})
)
})
)
})