-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
116 lines (108 loc) · 4.2 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
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
/* eslint-disable no-restricted-globals */
/**
* The worker is here to allow installation, its not
* here to for much more as doing anything complex makes
* reloading the worker hard.
*
* Not using preload as this causes failures when on an isolated network.
*
* to disable caching create a localStorage key 'disableCache' with any value.
*/
const CACHE_NAME = 'lifepo4';
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE_NAME);
cache.addAll([
'/',
]);
})());
});
self.addEventListener('beforeinstallprompt', (event) => {
console.debug('Before Event Install ', event);
});
let cacheEnabled = true;
self.addEventListener('message', (event) => {
if (event.data.cacheEnabled !== undefined) {
cacheEnabled = event.data.cacheEnabled;
console.log('Cache Enabled now ', cacheEnabled);
}
});
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
if (event.request.method === 'GET' && !url.pathname.startsWith('/api')) {
event.respondWith((async () => {
if (cacheEnabled
&& !event.request.headers.get('Authorization')
&& (event.request.destination === 'image'
|| event.request.destination === 'script'
|| event.request.destination === 'style'
|| event.request.destination === 'document'
|| event.request.destination === 'manifest')) {
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(event.request, { ignoreSearch: true });
if (cachedResponse) {
const date = cachedResponse.headers.get('date');
if (date
&& (Date.now() - Date.parse(date)) < 60000) {
console.debug('HIT', cachedResponse);
return cachedResponse;
}
cachedResponse.headers.forEach(console.debug);
console.debug('Expired', cachedResponse, (Date.now() - Date.parse(date)));
}
try {
// If the resource was not in the cache or too old in the cache try the network.
const fetchResponse = await fetch(event.request);
if (fetchResponse.status === 200) {
const cacheControl = fetchResponse.headers.get('cache-control');
if (!cacheControl
|| !(cacheControl.includes('private') || cacheControl.includes('no-store'))) {
// Save the resource in the cache and return it.
// create a new copy so that we can set the date header as Chrome Web Server
// doesnt set this.
// clone so that we can read the body.
const copy = fetchResponse.clone();
const headers = new Headers(copy.headers);
headers.set('date', new Date().toUTCString());
const body = await copy.blob();
const cacheResponse = new Response(body, {
status: fetchResponse.status,
statusText: fetchResponse.statusText,
headers,
ok: fetchResponse.ok,
});
console.debug('MISS', cacheResponse);
cache.put(event.request, cacheResponse);
return fetchResponse;
}
}
console.debug('PASS', fetchResponse);
} catch (e) {
console.debug('Network Failed ', e);
// The network failed.
}
if (cachedResponse) {
// serve stale if its available
console.debug('STALE', cachedResponse);
return cachedResponse;
}
}
try {
console.debug('Request ', event.request);
const passResponse = await fetch(event.request);
console.debug('PASS request', event.request);
for (const k of event.request.headers.keys()) {
console.debug(` ${k}: ${event.request.headers.get(k)}`);
}
console.debug('PASS response', passResponse);
for (const k of passResponse.headers.keys()) {
console.debug(` ${k}: ${passResponse.headers.get(k)}`);
}
return passResponse;
} catch (e) {
console.debug('Network Failed ', e);
}
return new Response('', { status: 504, statusText: 'offline' });
})());
}
});