0
      self.addEventListener('install', function (event) {
    console.log('SW Installed');
    event.waitUntil(
      caches.open('static')
        .then(function (cache) {
          cache.addAll([
            '/',
            '/static/js/renderjson.js',
            "/static/css/app.css",
            '/static/js/contribute.js',
            "http://pending.biothings.io/denovodb/metadata",
            "http://pending.biothings.io/ccle/metadata",
            "http://pending.biothings.io/kaviar/metadata",
            "http://pending.biothings.io/biomuta/metadata",
            "http://pending.biothings.io/fire/metadata",
          ]);
        })
    );
  });

  self.addEventListener('activate', function () {
    console.log('SW Activated');
  });

  self.addEventListener('fetch', function(event) {
    console.log('FETCH',event.request);
    event.respondWith(
      caches.match(event.request)
        .then(function(response) {
          if (response) {
                // retrieve from cache
                console.log('Found ', event.request.url, ' in cache');
                return response;
            }

            // if not found in cache, return default offline content (only if this is a navigation request)
            if (event.request.mode === 'navigate') {
                return caches.match('/');
            }

            // fetch as normal
            console.log('Network request for ', event.request.url);
            return fetch(event.request);
        })
    );
  });

我是 PWA 的新手,所以请耐心等待,我正在运行一个龙卷风服务器,这里是注册:

if ('serviceWorker' in navigator) {
  // Use the window load event to keep the page load performant
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('/static/js/worker.js');
    console.log('Service Worker registered');
  });
}

我放置了一些控制台日志以查看是否触发了 fetch 事件,但似乎没有发生任何事情,当我检查应用程序选项卡上的脱机框并刷新应用程序时,该应用程序无法脱机工作。所有资产都被正确缓存并验证它们在那里。看起来最后一点只是让 Worker 服务器成为正确的缓存文件。

4

1 回答 1

0

具体来说,对于我的情况,我们的网站只需要使用 https 来让工作人员正确激活。之后,听众完美地工作。我会推荐使用 Workbox,更容易!

于 2019-03-21T22:27:34.343 回答