Last active
October 19, 2022 10:15
-
-
Save jonnyom/9e763cbc38d7f9669b482f7e593d1f9a to your computer and use it in GitHub Desktop.
Experimenting with using JavaScript service workers hosted on a separate CDN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
importScripts('https://cdn.evervault.com/serviceWorker.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ('serviceWorker' in navigator) { | |
window.addEventListener('load', function () { | |
navigator.serviceWorker.register('customer-page.js', {scope: '/'}).then( | |
function (registration) { | |
console.log( | |
'ServiceWorker registration successful with scope: ', | |
registration.scope | |
); | |
}, | |
function (err) { | |
console.log('ServiceWorker registration failed: ', err); | |
} | |
); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- @format --> | |
<!DOCTYPE html> | |
<header> | |
<title>Service boi</title> | |
// include this to register service worker | |
<script src="https://cdn.jonny.com/register.js"></script> | |
<script> | |
window | |
.fetch('https://anything.hello.com', { | |
method: 'post', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ | |
email: '[email protected]', | |
social_identity: 'abc123', | |
name: 'bob', | |
}), | |
}) | |
.then((r) => { | |
console.log('Fetch happened', r); | |
}); | |
</script> | |
</header> | |
<body> | |
<p>Hello</p> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
serialize = (request) => { | |
let headers = {}; | |
for (let entry of request.headers.entries()) { | |
headers[entry[0]] = entry[1]; | |
} | |
let serialized = { | |
url: 'https://testing.com', | |
headers: headers, | |
method: request.method, | |
mode: request.mode, | |
credentials: request.credentials, | |
cache: request.cache, | |
redirect: request.redirect, | |
referrer: request.referrer, | |
}; | |
// Only if method is not `GET` or `HEAD` is the request allowed to have body. | |
if (request.method !== 'GET' && request.method !== 'HEAD') { | |
return request | |
.clone() | |
.text() | |
.then((body) => { | |
serialized.body = body; | |
return Promise.resolve(serialized); | |
}); | |
} | |
return Promise.resolve(serialized); | |
}; | |
self.addEventListener('fetch', (event) => { | |
let response; | |
if (event.request.url.includes('anything')) { | |
const newEvent = event.request.clone(); | |
response = serialize(event.request).then((serializedRequest) => { | |
return fetch(serializedRequest.url, serializedRequest).then((r) => { | |
return r; | |
}); | |
}); | |
} else { | |
response = fetch(event.request).then((r) => { | |
return r; | |
}); | |
} | |
event.respondWith(response); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jonnyom Haven't tried your code but were you able to register service-worker through this?