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
|
self.addEventListener('install', function(event) {
console.log ('installed service worker', event);
self.skipWaiting();
});
/* load stuff through service worker immediately? XXX: only debugging? */
self.addEventListener('activate', async function() {
if (self.registration.navigationPreload) {
// Enable navigation preloads!
await self.registration.navigationPreload.enable();
} /*event => {
event.waitUntil(clients.claim());*/
});
self.addEventListener('fetch', function(event) {
let origreq = event.request;
console.log ('fetch event', origreq.url, event);
let url = new URL (origreq.url);
url.protocol = 'https:';
url.port = 443;
url.hash = '';
if (url.hostname.endsWith ('.swayback.localhost')) {
url.hostname = url.hostname.slice (0, url.hostname.length-'.swayback.localhost'.length);
}
console.log ('orig url', url);
/* should contain everything we cannot use in the actual request (i.e. url and method) */
let body = {
'url': url.href,
'method': origreq.method,
};
let headers = {
'Content-Type': 'application/json',
};
/* add a few well-known request headers */
let origheaders = origreq.headers;
if (origheaders.has ('accept')) {
headers['Accept'] = origreq.headers.get ('accept');
}
console.log ('sending', body, headers);
let req = new Request ('http://swayback.localhost:5000/raw',
{method: 'POST', body: JSON.stringify (body), headers: headers,
mode: 'cors'});
event.respondWith (
fetch(req)
.then(function (response) {
// response may be used only once
// we need to save clone to put one copy in cache
// and serve second one
let responseClone = response.clone();
console.log ('got resp', responseClone);
return response;
})
.catch (function () {
console.log ('nope');
})
);
});
|