I'm trying to put a Service Worker on a website and I want to load the service worker script from a CDN. However when I load the service worker from a different domain I get the following error.
ServiceWorker DOMException: Failed to register a ServiceWorker: The origin of the provided scriptURL ('https://cdndomain.com') does not match the current origin ('http://mydomain')
Is there a way to load a Service worker from a CDN? I've seen few push notification services doing it out there, can we use eval to execute service worker js locally?
Any workarounds for this? Thanks
Here is how my current code looks like
if (navigator.serviceWorker) {
console.log("ServiceWorkerssupported");
navigator.serviceWorker.register('https://cdn.com/sw.js', {
scope: './'
})
.then(function(reg) {
console.log("ServiceWorkerstered", reg);
})
.catch(function(error) {
console.log("Failedegister ServiceWorker", error);
});
}