-
Notifications
You must be signed in to change notification settings - Fork 2
/
sw.js
134 lines (127 loc) · 4.25 KB
/
sw.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const version = 'v0.11.0e'
const assets = [
'/app/',
'/app/dist/density-plot.js',
'/app/dist/bundle.js',
'/app/dist/webppl.min.js',
'/app/dist/worker.js',
'/app/css/vue-material.css',
'/app/css/handsontable.full.min.css',
'/app/css/vue2-perfect-scrollbar.min.css',
'/app/css/roboto.css',
'/app/css/icons.css',
'/app/css/vis-network.min.css',
'/app/css/img/network/acceptDeleteIcon.png',
'/app/css/img/network/addNodeIcon.png',
'/app/css/img/network/backIcon.png',
'/app/css/img/network/connectIcon.png',
'/app/css/img/network/cross2.png',
'/app/css/img/network/cross.png',
'/app/css/img/network/deleteIcon.png',
'/app/css/img/network/downArrow.png',
'/app/css/img/network/editIcon.png',
'/app/css/img/network/leftArrow.png',
'/app/css/img/network/minus.png',
'/app/css/img/network/plus.png',
'/app/css/img/network/rightArrow.png',
'/app/css/img/network/upArrow.png',
'/app/css/img/network/zoomExtends.png',
'/app/images/android-icon-192x192.png',
'/app/images/favicon-16x16.png',
'/app/images/favicon-32x32.png',
'/app/images/favicon-96x96.png',
'/app/images/favicon.ico',
'/app/images/s.png',
'/app/fonts/MaterialIcons-Regular.ttf',
'/app/fonts/MaterialIcons-Regular.woff',
'/app/fonts/MaterialIcons-Regular.woff2',
'/app/fonts/Roboto-Bold.ttf',
'/app/fonts/Roboto-Bold.woff',
'/app/fonts/Roboto-Bold.woff2',
'/app/fonts/Roboto-Light.ttf',
'/app/fonts/Roboto-Light.woff',
'/app/fonts/Roboto-Light.woff2',
'/app/fonts/Roboto-Medium.ttf',
'/app/fonts/Roboto-Medium.woff',
'/app/fonts/Roboto-Medium.woff2',
'/app/fonts/Roboto-Regular.ttf',
'/app/fonts/Roboto-Regular.woff',
'/app/fonts/Roboto-Regular.woff2'
]
console.log('Service worker: version', version)
self.addEventListener('install', function (event) {
console.log('Service worker: Installation started')
event.waitUntil(
caches
.open(version)
.then(function (cache) {
return cache.addAll(assets)
})
.then(function () {
console.log(`Service worker: Installation completed. Cached ${assets.length} assets`)
})
)
})
self.addEventListener('activate', function (event) {
console.log('Service worker: Activation started')
event.waitUntil(
caches
.keys()
.then(function (keys) {
console.log('Service worker: Caches', keys)
return Promise.all(
keys
.filter(function (key) {
return !key.startsWith(version)
})
.map(function (key) {
console.log('Service worker: Remove cache', key)
return caches.delete(key)
})
)
})
.then(function () {
console.log('Service worker: Activation completed')
})
)
})
self.addEventListener('fetch', function (event) {
if (event.request.method !== 'GET') {
console.log('Service worker: fetch event ignored', event.request.method, event.request.url)
return
}
event.respondWith(
caches
.match(event.request)
.then(function (cached) {
const networked = fetch(event.request)
.then(fetchedFromNetwork, unableToResolve)
.catch(unableToResolve)
console.log('Service worker: fetch result ', cached ? '(cached)' : '(network)', event.request.url)
return cached || networked
function fetchedFromNetwork (response) {
const cacheCopy = response.clone()
// console.log('Service worker: fetch response from network', event.request.url)
caches
.open(version)
.then(function add (cache) {
cache.put(event.request, cacheCopy)
})
.then(function () {
// console.log('Service worker: fetch response stored in cache', event.request.url)
})
return response
}
function unableToResolve () {
console.log('Service worker: fetch request failed in both cache and network')
return new Response('<h1>StatSim: Service Unavailable</h1><p>The page is not cached. Check your network connection.</p>', {
status: 503,
statusText: 'Service Unavailable',
headers: new Headers({
'Content-Type': 'text/html'
})
})
}
}) // *caches.match.then
) // *respondwith
})