Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register service worker failed with localhost

I have the following error when I try to register a service worker in a basic app served by a node Express V4 server / on Chrome 42:

DOMException: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. {message: "Failed to register a ServiceWorker: A 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 to

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

  }
like image 851
benek Avatar asked Apr 18 '15 13:04

benek


People also ask

Can service worker run on localhost?

Service workers will register and work just fine as long as you access the localhost domain -- without HTTPS.

How do I register a serviceWorker?

register. The code first checks to see if the serviceWorker object exists in the browser's navigator object. If it does, then this browser supports service workers. After the code verifies it can register the service worker, it does so through the call to navigator.

What is a service worker?

A service worker is responsible for assisting the community welfare development by providing social services to an organization or specific individual groups, supporting their needs, and addressing their community concerns.


2 Answers

I think You are trying to Register non-existent script. In this case this issue comes. Please check your script path and scope path. Maybe you don't have any 'worker.js' in the directory where this script exists. If this is the case, please provide full path or provide worker.js in same directory.

like image 51
Paritosh Avatar answered Sep 23 '22 19:09

Paritosh


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

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

However I had not told express the location of any other files I wanted it to be able to use. At this stage my only other file was the service worker file which was sitting at 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 whether your issue is with the server itself you could download Web Server for Chrome and point it at the root directory of your app. Check the server is started and click on the Web Server URL. If your service worker registration now succeeds you'll know it's a problem with your express server setup not your service worker registration code.

like image 23
Natstar Avatar answered Sep 21 '22 19:09

Natstar