服务人员不允许PWA脱机显示

时间:2019-02-15 18:03:16

标签: javascript jquery html service-worker progressive-web-apps

我有一个PWA无法离线工作。在线工作效果很好,但是当我切换到离线状态时,它什么也没做。我怀疑它与我的service-worker.js文件有关-

var dataCacheName = 'sigmaApp';
var cacheName = 'sigmaApp';
var filesToCache = [
  "/", 
  "./images/icons/icon-128x128.png",
  "./images/icons/icon-144x144.png",
  "./images/icons/icon-152x152.png",
  "./images/icons/icon-192x192.png",
  "./images/icons/icon-256x256.png",
  "./images/create-how-boxes.png",
  "./images/create-how-lines.png",
  "./images/create-what-logo-1.png",
  "./images/create-what-logo-2.png",
  "./images/create-who-telkomsel.png",
  "./images/create-who-volvo.png",
  "./images/create-who-windstream.png",
  "./images/create-why-hand.png",
  "./images/create-why-rock.png",
  "./images/deliver-what-logo-1.png",
  "./images/deliver-what-logo-2.png",
  "./images/deliver-why-road-orange.png",
  "./images/deliver-why-roads.png",
  "./images/sell-how-phone.png",
  "./images/sell-how-sphere.png",
  "./images/sell-what-logo-1.png",
  "./images/sell-what-logo-1.png",
  "./images/sell-what-logo-2.png",
  "./images/sell-why-block.png",
  "./images/sell-why-hand.png",
  "./images/telkomsel-logo.png",
  "./scripts",
  "./index.html",
  "./manifest.json",
  "./scripts/jquery-3.3.1.js",
  "./scripts/swiper.min.js",
  "./scripts/swiper.min.js.map",
  "./scripts/tube-animation.min.js",
  "./scripts/typeit.min.js",
  "./scripts/scripts.js",
  "./service-worker.js",
  "./styles",
  "./styles/style.min.css",
  "./videos/create-what-infographic-1.mp4",
  "./scripts/swiper.min.js.map"
];

self.addEventListener('install', function(e) {
  console.log('[ServiceWorker] Install');
  e.waitUntil(
    caches.open(cacheName).then(function(cache) {
      console.log('[ServiceWorker] Caching app shell');
      return cache.addAll(filesToCache);
    })
  );
});

self.addEventListener('activate', function(e) {
  console.log('[ServiceWorker] Activate');
  e.waitUntil(
    caches.keys().then(function(keyList) {
      return Promise.all(keyList.map(function(key) {
        if (key !== cacheName && key !== dataCacheName) {
          console.log('[ServiceWorker] Removing old cache', key);
          return caches.delete(key);
        }
      }));
    })
  );
  return self.clients.claim();
});

self.addEventListener('fetch', function(e) {
  console.log('[Service Worker] Fetch', e.request.url);
  e.respondWith(
    caches.match(e.request).then(function(response) {
      return response || fetch(e.request);
    })
  );
});

我的页面似乎出错了-未捕获(承诺)TypeError:安装事件侦听器中的service-worker.js文件中的请求失败。我觉得它正在通过filesToCache数组并在那里出错。

有人对我在做什么错有任何想法吗?

1 个答案:

答案 0 :(得分:1)

原来这是我的错。我不小心添加了两个相同的文件,因此它因此出错了。

"./images/sell-what-logo-1.png",
"./images/sell-what-logo-1.png",

希望这对其他人有帮助