service-worker.js不提供缓存的文件吗?

时间:2019-12-18 16:21:41

标签: javascript service-worker

我试图让service-worker.js可以在我的应用程序中缓存文件,并且我从https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API获得了代码。

它已安装且处于活动状态,但是我不知道它是否正在从缓存中获取文件,因为我只在试图缓存的文件上获得状态200。

我的根目录中有sw.js文件。而我的myapp.js文件位于我的root / js / myapp.js中 所以在我的js文件中我有这个。

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js', { scope: '/' }).then(function(reg) {

    if(reg.installing) {
      console.log('Service worker installing'); //So it writes this to the console
    } else if(reg.waiting) {
      console.log('Service worker installed');//So it writes this to the console
    } else if(reg.active) {
      console.log('Service worker active');//So it writes this to the console
    }

  }).catch(function(error) {
    // registration failed
    console.log('Registration failed with ' + error);
  });
}

sw.js文件如下所示。

self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open('v2').then(function(cache) {
      return cache.addAll([
        '/css/v5/framework7.bundle.min.css',
        '/css/v5/my-app-v5.css',
        '/css/addtohomescreen.css',
        '/css/sumoselect.css',
        '/css/icons.css',
        '/js/v5/framework7.bundle.min.js',
        '/js/addtohomescreen.js',
        '/js/jquery.cookie.js',
        '/js/jquery.sumoselect.min.js',
        '/js/wookmark.min.js',
        '/js/imagesloaded.pkgd.min.js',
        '/js/exif/exif.min.js',
        '/js/exif/load-image.min.js',
        '/js/exif/load-image-scale.min.js',
        '/js/exif/load-image-orientation.min.js',
        '/images/ikoner/100x100.png',
        '/images/ikoner/200x200.png',
        '/images/ikoner/192x192.png',



      ]);
    })
  );
});

在chrome inspector(网络)中,我可以看到它正在获取文件,但是它们没有被缓存,我得到200状态,类型是样式表,Initiator表示它是从我的索引页而不是从索引页获取文件缓存?

那么我怎么知道是否从缓存中提供文件?

我还能有2个不同的service-worker文件吗?我也有一个来自pushwoosh的程序,叫做pushwoosh-service-worker.js,但是在测试此程序时,我暂时不使用它。

感谢任何输入,谢谢。

0 个答案:

没有答案