0

我已经向我的服务人员添加了谷歌代码,但它在谷歌控制台中给了我错误......

控制台谷歌中的错误:

pwaupdate:176 Uncaught (in promise) TypeError: 无法使用脚本 ('/pwabuilder-sw.js') 为范围 (' https://myweb.org/ ') 注册 ServiceWorker: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);
  });
}
4

1 回答 1

2

这是一个简单的错误 =)

错误意味着:脚本文件有问题,它不是有效的 JavaScript。

我很确定这条线是罪魁祸首。这不是有效的代码:

  return http://caches.open(CACHE).then(function (cache) {
于 2020-05-29T18:51:14.533 回答