-
Notifications
You must be signed in to change notification settings - Fork 2
/
sw.js
149 lines (129 loc) · 4.08 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const cacheName = 'dokuwiki PWA cache';
self.addEventListener('install', function (e) {
idbKeyval.del('lastSync');
});
self.addEventListener('message', function (e) {
// console.log("[ServiceWorker] Received Message:");
// console.log(e.data.type);
switch (e.data.type) {
case 'updatePages':
cachePages(e, e.data.pages);
break;
case 'getLastUpdate':
idbKeyval.get('lastSync').then((value) => e.source.postMessage(
{
type: 'lastUpdate',
ts: value,
}
));
break;
case 'getHashVersion':
e.source.postMessage(
{
type: 'swHashVersion',
hash: swHashVersion, // injected when serviceworker is constructed in PHP
}
)
}
});
function cachePages(e, data) {
idbKeyval.set('lastSync', Math.floor(Date.now()/1000));
e.waitUntil(
caches.open(cacheName).then(function (cache) {
// if (r) {
// const lmTimeString = r.headers.get('Last-Modified');
// const ts = (new Date( lmTimeString )).getTime();
// console.log('we have a cache for ' + e.request.url + ' from ', lmTimeString, ts);
// }
return Promise.all(data.map(function (pageData) {
return cache.add(pageData.link)
}));
})
);
}
self.addEventListener('activate', function (e) {
console.log('[ServiceWorker] Activate');
});
const CACHED_DESTINATIONS = [
'document',
'style',
'script',
'image',
'font',
];
const OFFLINE_FIRST_DESTINATIONS = [
'script',
'style',
'font',
];
self.addEventListener('fetch', function (e) {
if (e.request.method !== 'GET') {
return;
}
if (!CACHED_DESTINATIONS.includes(e.request.destination)) {
// only cache important modes
return;
}
if (OFFLINE_FIRST_DESTINATIONS.includes(e.request.destination)) {
e.respondWith(cacheFirst(e.request));
return;
}
e.respondWith(networkFirst(e.request));
});
function networkFirst(request) {
return fromNetwork(request, 400).then(function (response) {
if (response.headers.has('X-DWPLUGIN-PWAOFFLINE-ACT') &&
response.headers.get('X-DWPLUGIN-PWAOFFLINE-ACT') !== 'show') {
// don't cache modes other than show
return response;
}
return caches.open(cacheName).then(function (cache) {
cache.put(request, response.clone());
return response;
});
}).catch(function () {
return fromCache(request);
})
}
function cacheFirst(request) {
return caches.match(request).then(function (cacheResponse) {
return cacheResponse || fetch(request).then(function (response) {
return caches.open(cacheName).then(function (cache) {
cache.put(request, response.clone());
return response;
});
});
})
}
function fromNetwork(request, timeout) {
return new Promise(function (fulfill, reject) {
const timeoutId = setTimeout(reject, timeout);
fetch(request).then(function (response) {
if (response.status >= 500) {
reject();
}
clearTimeout(timeoutId);
fulfill(response);
}, reject);
});
}
function fromCache(request) {
if (!CACHED_DESTINATIONS.includes(request.destination)) {
return Promise.reject('no-match');
}
return caches.open(cacheName).then(function (cache) {
return cache.match(request).then(function (matching) {
if (matching) {
return matching;
}
if (request.destination === 'document') {
return new Response('Page not available. Please go back.', {
headers: {
'Content-Type': 'text/plain'
}
});
}
return Promise.reject('no-match');
});
});
}