首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ServiceWorker脚本评估失败

ServiceWorker脚本评估失败
EN

Stack Overflow用户
提问于 2020-05-29 08:26:11
回答 1查看 2.1K关注 0票数 0

我已经将google代码添加到我的服务工人中,但它在google控制台中显示错误...

控制台Google中的错误:

pwaupdate:176未捕获(承诺中) TypeError:无法使用脚本(‘/pwabuilder-sw.js’)注册作用域('https://myweb.org/')的ServiceWorker : ServiceWorker脚本评估失败

我的服务工作者:

代码语言:javascript
复制
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);
  });
}
EN

回答 1

Stack Overflow用户

发布于 2020-05-30 02:51:15

这是一个简单的bug =)

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

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

代码语言:javascript
复制
  return http://caches.open(CACHE).then(function (cache) {
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62076693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档