From 5d1bd2891183e93f43f5fdb67e0ffcefabcc8eed Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Fri, 16 Mar 2018 15:06:38 +0100 Subject: Configureable domain --- swayback/templates/sw.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 swayback/templates/sw.js (limited to 'swayback/templates') diff --git a/swayback/templates/sw.js b/swayback/templates/sw.js new file mode 100644 index 0000000..ea653a9 --- /dev/null +++ b/swayback/templates/sw.js @@ -0,0 +1,61 @@ +/* script config, dynamically generated */ +const baseHost = "{{ baseHost }}"; +const baseDomain = baseHost.split (':', 1)[0]; + +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 ('.' + baseDomain)) { + url.hostname = url.hostname.slice (0, url.hostname.length-('.'+baseDomain).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://' + baseHost + '/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'); + }) + ); +}); -- cgit v1.2.3