Worker registration error

I am currently working on a working service for handling push notifications in a browser. I am currently experiencing a "Registration Registration Error" error. Can anyone help to solve this problem? Check the client1.html and service-worker.js file below:

ERROR:

SW registration error with SecurityError error: Failed to register ServiceWorker: The protocol of the current source URL ("null") is not supported.

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); }); 

client1.html

  <!doctype html> <html> <head> <title>Client 1</title> </head> <body> <script> if('serviceWorker' in navigator){ // Register service worker navigator.serviceWorker.register('service-worker.js').then(function(reg){ console.log("SW registration succeeded. Scope is "+reg.scope); }).catch(function(err){ console.error("SW registration failed with error "+err); }); } </script> </body> </html> 
+6
source share
1 answer

Solved: Firstly, the service worker works only in safe mode, either in https or in localhost. It does not work on local resources like file: // or http.

and the second question was during registration.

  navigator.serviceWorkerContainer.register('service-worker.js').then(function(reg){ 
+11
source

All Articles