Ошибка при оценке сценария ServiceWorker - PullRequest
0 голосов
/ 29 мая 2020

Я добавил код Google к своему сервисному работнику, но это дает мне ошибку в консоли Google ...

Ошибка в консоли Google:

pwaupdate: 176 Uncaught (в обещании) TypeError : Не удалось зарегистрировать ServiceWorker для области ('https://myweb.org/') с помощью скрипта ('/pwabuilder-sw.js'): Ошибка оценки скрипта ServiceWorker

Мой сервисный работник:

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);
  });
}

1 Ответ

1 голос
/ 29 мая 2020

Это простая ошибка =)

Ошибка означает: проблема в файле сценария, это НЕ действительный JavaScript.

Я почти уверен, что виновата эта строчка. Это недействительный код:

  return http://caches.open(CACHE).then(function (cache) {
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...