siken
siken

Reputation: 19

ServiceWorker script evaluation failed

I have added google code to my service worker but it gives me error in google console...

The error in console Google:

pwaupdate:176 Uncaught (in promise) TypeError: Failed to register a ServiceWorker for scope ('https://myweb.org/') with script ('/pwabuilder-sw.js'): ServiceWorker script evaluation failed

My service worker:

const CACHE = "pwabuilder-offline-page";

const offlineFallbackPage = "/offline.html";

// Install stage sets up the offline page in the cache and opens a new cache
self.addEventListener("install", function (event) {
  console.log("[PWA Builder] Install Event processing");

  event.waitUntil(
    http://caches.open(CACHE).then(function (cache) {
      console.log("[PWA Builder] Cached offline page during install");

      if (offlineFallbackPage === "offline.html") {
        return cache.add(new Response("TODO: Update the value of the offlineFallbackPage constant in the serviceworker."));
      }

      return cache.add(offlineFallbackPage);
    })
  );
});

//https://developers.google.com/web/updates/2017/02/navigation-preload
//Acelerar al trabajador de servicio con precargas de navegación
self.addEventListener('fetch', event => {
  event.respondWith(async function() {
    // Respond from the cache if we can
    const cachedResponse = await caches.match(event.request);
    if (cachedResponse) return cachedResponse;

    // Else, use the preloaded response, if it's there
    const response = await event.preloadResponse;
    if (response) return response;

    // Else try the network.
    return fetch(event.request);
  }());
});

function fromCache(request) {
  // Check to see if you have it in the cache
  // Return response
  // If not in the cache, then return the offline page
  return http://caches.open(CACHE).then(function (cache) {
    return cache.match(request).then(function (matching) {
      if (!matching || matching.status === 404) {
        // The following validates that the request was for a navigation to a new document
        if (request.destination !== "document" || request.mode !== "navigate") {
          return Promise.reject("no-match");
        }

        return cache.match(offlineFallbackPage);
      }

      return matching;
    });
  });
}

function updateCache(request, response) {
  return http://caches.open(CACHE).then(function (cache) {
    return cache.put(request, response);
  });
}

Upvotes: 1

Views: 10351

Answers (1)

pate
pate

Reputation: 5253

It's a simple bug =)

The error means: there's a problem in the script file, it is NOT valid JavaScript.

I'm pretty sure this line is the culprit. This is not valid code:

  return http://caches.open(CACHE).then(function (cache) {

Upvotes: 3

Related Questions