ilan-schemoul / meteor-service-worker

An universal service worker for meteor apps
MIT License
137 stars 26 forks source link

Request scheme 'chrome-extension' is unsupported #23

Closed jankapunkt closed 1 month ago

jankapunkt commented 3 years ago

I am using the Meteor Devtools Evolved Extension and the sw always fails when the extension is loaded:

event.request is chrome-extension://ibniinmoafhgbifjojidlagmggecmpgf/build/inject.js and I get

sw.js:64 Uncaught (in promise) TypeError: Failed to execute 'put' on 'Cache': Request scheme 'chrome-extension' is unsupported
    at sw.js:64
(anonymous) @ sw.js:64

Should I add a test on the request to contain http(s):// ? Potential fix would be

if (event.request.method !== 'POST' && event.request.url.startsWith('http')) {
  caches.open(version).then(cache => cache.put(event.request, clonedResponse))
}
mullojo commented 2 years ago

I did find an easy solution for this error which was also a common solution for many other Service Workers where others were reporting this error.

Inside the fetch event listener:

self.addEventListener('fetch', (event) => {});

The easiest approach was to inspect each event.request.url and filter out the chrome-extention:// urls by accepting only http & https urls.

// inspect each event request url in the console
console.log("event request startsWith & url:", event.request.url.startsWith('http'), event.request.url);

To do this we just wrap the main code inside the fetch event listener with a conditional statement


 if (event.request.url.startsWith('http')){ 
  // only process http & https requests, prevents chrome-extention errors

      const requestToFetch = event.request.clone();
      event.respondWith();

  }

This worked nicely, so I went ahead and created a PR for it here: https://github.com/NitroBAY/meteor-service-worker/pull/24

ilan-schemoul commented 2 years ago

@jankapunkt is it working for you ?

ilan-schemoul commented 1 month ago

Merged