1

I'm trying to Use a CDN network to host my service worker which allow cross-origin CORPs. Then I came To know About fetch API to access cross Origin Resources. Here Code Which I'm using on my Index.HTML to register service worker

if ('serviceWorker' in navigator) { window.addEventListener('load', function() { navigator.serviceWorker.register('https://www.otherdomain.com/sw.js').then(function(registration) { // Registration was successful console.log('ServiceWorker registration successful with scope: ', registration.scope); }, function(err) { // registration failed :( console.log('ServiceWorker registration failed: ', err); }); }); }` 

How To Use Cross cross-domain Service worker with fetch API

1 Answer 1

1

What error are you getting the service worker only working trough https anyways would be beneficial not to include this into your html file but rather in another js file.

Here is something that would call you sw.js file. include this into your html or again another js file.

var deferredPrompt; if (!window.Promise) { window.Promise = Promise; } if ('serviceWorker' in navigator) { navigator.serviceWorker .register('sw.js') .then(function () { console.log('Service worker registered!'); }) .catch(function(err) { console.log(err); }); } window.addEventListener('beforeinstallprompt', function(event) { console.log('beforeinstallprompt fired'); event.prompt(); deferredPrompt = event; return false; }); 

Try This

self.addEventListener('fetch', function (event) { var url = 'https://httpbin.org/get'; //add your URL here if (event.request.url.indexOf(url) > -1) { event.respondWith( caches.open(CACHE_DYNAMIC_NAME) .then(function (cache) { return fetch(event.request) .then(function (res) { // trimCache(CACHE_DYNAMIC_NAME, 3); cache.put(event.request, res.clone()); return res; }); }) ); } else if (isInArray(event.request.url, STATIC_FILES)) { event.respondWith( caches.match(event.request) ); } else { event.respondWith( caches.match(event.request) .then(function (response) { if (response) { return response; } else { return fetch(event.request) .then(function (res) { return caches.open(CACHE_DYNAMIC_NAME) .then(function (cache) { // trimCache(CACHE_DYNAMIC_NAME, 3); cache.put(event.request.url, res.clone()); return res; }) }) .then(function (err) { return caches.open(CACHE_STATIC_NAME) .then(function (cache) { if (event.request.headers.get('accept').includes('text/html')) { return cache.match('offline.html'); } }); }); } }) ); } }); self.addEventListener('install', function (event) { console.log('[Service Worker] Installing Service Worker ...', event); event.waitUntil( caches.open(CACHE_STATIC_NAME) .then(function (cache) { console.log('[Service Worker] Precaching App Shell'); cache.addAll(STATIC_FILES); }) ) }); 
Sign up to request clarification or add additional context in comments.

11 Comments

can you add your whole service worker? As I understand You trying to fetch the service worker from somewhere rather then from your local recources? Just need more information.
did you add the cors headers? Just need more info then couple of lines of code.
added more stuff in my answer try using fetch event rather then load. delete some of the stuff that you dont need you need to add static cache and dynamic cache as well. Is the issue only with your sw ?
well you dont install it anywhere in your code you just fetch it sofar
you need to install it activate and then fetch whatever you want to
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.