2

I have an apache server running. The backend as such is supported by django but placed on apache server. I have a service worker which otherwise works completely fine. Here's the code which does the registration

navigator.serviceWorker.register('/static/js/components/sw.js').then(function() {
                            return navigator.serviceWorker.ready;
                        }).then(function(){console.log("success")})

But the above code somehow never gives any response. The promise remains in pending state. Even adding a catch at the end doesn give any error. It goes to the navigator.serviceworker.ready but doesnt not resolve into the then function. I'm not sure what is the problem.

I've tried the following stuffs:

  • I though the problem may be that service workers needs to be in same origin and all so i added header called Service-Worker-Allowed:/static but that didnt do the job.

  • When i accessed the file by directly going to the path were html is saved (eg: mywebsite/static/serviceworker.html) after adding Service-Worker-Allowed: /static It worked fine. But when i accessed the same html file by using the url mapped by django(i.e the url which i mapped through urls.py, eg: mywebsite/admin/worker ), it doesn seem to work.

  • Tried to give relative paths, but relative paths doesn work in my case.

  • I put this on another apache server and there it seems to work fine.

I'm out of all ideas. Is this some django problem?

prajnavantha
  • 1,111
  • 13
  • 17
  • 1
    http://stackoverflow.com/a/35780776/385997 applies to your question as well. – Jeff Posnick Mar 23 '16 at 20:09
  • But it doesnt say the problem which i am facing. If i use absolute path (mywebsite/service_worker.html) , then is works fine. But if i give the path which i put in django urls.py and access the same html by using some other url(mywebsite/admin/worker/) then it doesn work even though its the same file. Scope should be same in both the cases then right? It will always be /static/somedirectory. But it doen work – prajnavantha Mar 24 '16 at 06:54
  • Got the answer from this post http://stackoverflow.com/questions/34389485/implementing-push-notification-using-chrome-in-django – prajnavantha Mar 29 '16 at 05:47

1 Answers1

0

I got the answer to the above question in this link Implementing push notification using chrome in Django Only thing is i gave the path of the sw.js file through urls.py file.

Basically the idea is that service worker registration requires the worker to be present in the same domain, something like

yourdomain/path_which_you_gave_during_registration

or if you have set a Service-Worker-Allowed: /path then it will be

yourdomain/path/path_which_you_gave_during_registration

Thus you need to do the appropriate changes for your service worker to be available.

Community
  • 1
  • 1
prajnavantha
  • 1,111
  • 13
  • 17