Error with Service Worker registration

自作多情 提交于 2019-12-14 03:53:58

问题


I'm trying to register a service worker but I have the following error:

Failed to register a ServiceWorker: The URL Protocol of the current origin ('null') is not supported

I'm under localhost with node js and I have my index.html and service-worker.js in the same folder

index.html

<!doctype html>
  <html>
  <head>
  <title>TryService</title>
  </head>
  <body>
  PROVA SITO CON SERVICE WORKER
  <script>
     if('serviceWorker' in navigator){
        // Register service worker
         navigator.serviceWorker.register('service-worker.js').then(function(reg){
            console.log("Registration OK!. Scope is "+reg.scope);
        }).catch(function(err){
            console.error("Registration FAILED! "+err);
        });
    }
  </script>

  </body>
  </html>

service-worker.js

console.log('Started', self);
self.addEventListener('install', function(event) {
  self.skipWaiting();
  console.log('Installed', event);
});
self.addEventListener('activate', function(event) {
console.log('Activated', event);
});
self.addEventListener('push', function(event) {
  console.log('Push message received', event);
});

How can I fix it? Thank you in advance!

来源:https://stackoverflow.com/questions/42693552/error-with-service-worker-registration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!