Failed to start the working registry service using localhost

I have the following error when trying to register a service worker in the base application served by the node Express V4 / server on Chrome 42:

DOMException: Failed to register ServiceWorker: Bad HTTP response (404) was received while retrieving the script. {message: "Failed to register ServiceWorker: bad HTTP res ... code (404) was received when fetching the script.", name: "NetworkError", code: 19, INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2 ...} code : 19 message: "Failed

Here is the register code:

if ('serviceWorker' in navigator){ console.log("SW present !!! "); navigator.serviceWorker.register('worker.js', { //scope: '/toto/' }).then(function(registration){ console.log('Service worker registered : ', registration.scope); }) .catch(function(err){ console.log("Service worker registration failed : ", err); }); } 
+5
source share
2 answers

I think you are trying to register a nonexistent script. In this case, this problem occurs. Check the path to the path and scope of the script. Perhaps you do not have any "worker.js" in the directory where this script exists. If so, provide the full path or specify employee.js in the same directory.

+9
source

I also had this error when using the express server. It turns out that the problem is with the server setup itself, and not with the registration code of the service worker. I told my express application to get index.html as the default root using:

 app.get('/', function(req, res) { res.sendFile(path.join(__dirname + '/index.html')); }); 

However, I did not say that I can specify the location of other files that I would like to use. At this point, my only other file was the worker file that was in the root of the directory, so I fixed the problem by adding this line to the server file:

 app.use(express.static(__dirname + '/')); 

To debug the question of whether your problem is related to the server itself, you can download the web server for Chrome and point it to the root directory of your application. Verify the server is starting up and click the URL of the web server. If the registration of your service worker is successful, you will find out that the problem with setting up your express server is not your registration code for the service worker.

0
source

All Articles