Problem with using Service Worker(A Serviceworker passed a Promise to Fetchevent.respondWith() that resolved with non-Response value 'Undefined'.)

Asked

Viewed 70 times

0

The service worker that I will show below works well in google Chrome, but when I am using the mozila firefoz returns the error:

Failed to load 'http://127.0.0.1:8000/#/'. A ServiceWorker passed a promise to FetchEvent.respondWith() that resolved with non-Response value 'undefined'.

This way I get my site limited to Chrome. I would like help to have the firefox audience too. Tó using locally for service worker testing, so is 127.0.0.1:8000.

self.addEventListener("fetch", function(e) {
  const url = new URL(e.request.url);
  if (
    url.hostname === "https://meu.site.com" ||
    url.hostname === "cdnjs.cloudflare.com" ||
    url.hostname === "fonts.googleapis.com" ||
    url.hostname === "https://meu.site.homologacao.com/"
  ) {
    e.respondWith(
      caches.match(e.request).then(function(response) {
        var fetchRequest = e.request.clone();
        return fetch(fetchRequest)
          .then(function(response) {
            if (
              response ||
              response.status === 200 ||
              response.type === "basic"
            ) {
              console.log("OnLine");
              return response;
            } else {
              console.log("OffLine");
              var responseToCache = response.clone();
              caches.open(STATIC_CACHE_NAME).then(function(cache) {
                cache.put(e.request, responseToCache);
              });
              return response;
            }
          })
          .catch(error => {
            console.log("Fetch failed; returning offline page instead.", error);
            return caches.match(OFFLINE_URL);
          });
      })
    );
  } else if (CACHE_APP.indexOf(url.pathname) !== -1) {
    e.respondWith(caches.match(e.request));
  }
});

Someone could help me with this mistake?

1 answer

0


Sorry. I fixed the problem by adding the 127.0.0.1 url Stayed like this:

self.addEventListener("fetch", function(e) {
  const url = new URL(e.request.url);
  if (
    url.hostname === "https://meu.site.com" ||
    url.hostname === "cdnjs.cloudflare.com" ||
    url.hostname === "fonts.googleapis.com" ||
    url.hostname === "https://meu.site.homologacao.com/"
  ) {
    e.respondWith(
      caches.match(e.request).then(function(response) {
        var fetchRequest = e.request.clone();
        return fetch(fetchRequest)
          .then(function(response) {
            if (
              response ||
              response.status === 200 ||
              response.type === "basic"
            ) {
              console.log("OnLine");
              return response;
            } else {
              console.log("OffLine");
              var responseToCache = response.clone();
              caches.open(STATIC_CACHE_NAME).then(function(cache) {
                cache.put(e.request, responseToCache);
              });
              return response;
            }
          })
          .catch(error => {
            console.log("Fetch failed; returning offline page instead.", error);
            return caches.match(OFFLINE_URL);
          });
      })
    );
  } else if (CACHE_APP.indexOf(url.pathname) !== -1) {
    e.respondWith(caches.match(e.request));
  }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.