JavaScript服务工作程序无法注册

JavaScript服务工作程序无法注册,javascript,python,service-worker,Javascript,Python,Service Worker,我无法让服务人员工作 我尝试了很多解决方案,但没有任何效果 我试图做的是缓存文件以供脱机使用 顺便说一句,这是我第一次和服务人员一起工作 main.js: if ('serviceWorker' in navigator) { navigator.serviceWorker .register('/sw.js') .catch(function(err) { console.error(err); }); } const files = [ '/', '/ind

我无法让服务人员工作
我尝试了很多解决方案,但没有任何效果
我试图做的是缓存文件以供脱机使用
顺便说一句,这是我第一次和服务人员一起工作

main.js

if ('serviceWorker' in navigator) {
   navigator.serviceWorker
   .register('/sw.js')
   .catch(function(err) {
   console.error(err);
});
}
const files = [
  '/',
  '/index.html',
  '/restaurant.html',
  '/css/styles.css',
  '/js/dbhelper.js',
  '/js/main.js',
  '/js/restaurant_info.js',
  '/data/restaurants.json',
  '/img/1.jpg',
  '/img/2.jpg',
  '/img/3.jpg',
  '/img/4.jpg',
  '/img/5.jpg',
  '/img/6.jpg',
  '/img/7.jpg',
  '/img/8.jpg',
  '/img/9.jpg',
  '/img/10.jpg'
];

self.addEventListener('install', function(e) {
  e.waitUntil(
      caches.open('v1').then(function(cache) {
          return cache.addAll(files);
      })
  );
});

self.addEventListener('fetch', function(e) {
  e.respondWith(
      caches.match(e.request).then(function(response) {
          if (response) {
          console.log('Found ', e.request, ' in cache');
          return response;
      }
      else {
          console.log('Could not find ', e.request, ' in cache, FETCHING!');
          return fetch(e.request)
          .then(function(response) {
              const clonedResponse = response.clone(); 
              caches.open('v1').then(function(cache) {
                  cache.put(e.request, clonedResponse);
              })
              return response;
          })
          .catch(function(err) {
              console.error(err);
          });
      }
  })
  );
});
sw.js

if ('serviceWorker' in navigator) {
   navigator.serviceWorker
   .register('/sw.js')
   .catch(function(err) {
   console.error(err);
});
}
const files = [
  '/',
  '/index.html',
  '/restaurant.html',
  '/css/styles.css',
  '/js/dbhelper.js',
  '/js/main.js',
  '/js/restaurant_info.js',
  '/data/restaurants.json',
  '/img/1.jpg',
  '/img/2.jpg',
  '/img/3.jpg',
  '/img/4.jpg',
  '/img/5.jpg',
  '/img/6.jpg',
  '/img/7.jpg',
  '/img/8.jpg',
  '/img/9.jpg',
  '/img/10.jpg'
];

self.addEventListener('install', function(e) {
  e.waitUntil(
      caches.open('v1').then(function(cache) {
          return cache.addAll(files);
      })
  );
});

self.addEventListener('fetch', function(e) {
  e.respondWith(
      caches.match(e.request).then(function(response) {
          if (response) {
          console.log('Found ', e.request, ' in cache');
          return response;
      }
      else {
          console.log('Could not find ', e.request, ' in cache, FETCHING!');
          return fetch(e.request)
          .then(function(response) {
              const clonedResponse = response.clone(); 
              caches.open('v1').then(function(cache) {
                  cache.put(e.request, clonedResponse);
              })
              return response;
          })
          .catch(function(err) {
              console.error(err);
          });
      }
  })
  );
});
这些文件是使用
python-mhttp.server


最好将文本消息粘贴到此处。如何承载
sw.js
?@jonaswills localhost,python serverWell“不受支持的MIME类型”表示服务器发送错误响应。服务人员需要HTTPS,您的开发环境中是否启用了HTTPS?