1

I've been delving into the emerging PUSH API and manage to register the service-worker as expected. In broad lines I follow the Mozilla recipes for this.

Generally speaking, you will create two javascript files; one for the service worker that you want to register, and one to perform the actual registration - lets call this index.js-, which in my case includes a registerWorker() function. I include an html file (say push.html) that calls this function on-load, so that browser that calls this html file will register the service worker. So far this all works fine in the development environment.

The problems start when I want to port this to my server. If I port the service-worker and the index.js to my server (e.g an Apache 2 server), I suddenly get a message telling me that the serviceworker is not in the navigator. I've been trying different strategies, but so far without succes.

My question therefore is; what is the best way to put the required javascript files on the server in order to achieve the above?

Edit:

I'll add the push.html file below:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Simple Push Notification - ServiceWorker Cookbook</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body onload="registerServiceWorker(12)">
<script src="./js/index.js"></script>
</body>
</html>

As far as I can see, the only difference is that I call this file from localhost in the dev environment, and http://{myserver}:{myport}// from the server. I would not expect any different behaviour unless maybe it has something to do with SSL requirements or CORS filters...

Edit 2:

ok..I found the most probable cause, based on this post: Can't find serviceWorker in navigator anymore

When putting the code on the server, https support is required!

keesp
  • 301
  • 2
  • 13
  • is this apache-specific? because I think you cannot do this in apache server..But again I might be wrong – Nikos M. Dec 21 '18 at 13:35
  • 1
    No.. I actually run on a Jetty Server (also in the dev environment) , but as far as I know registration should be pretty server-agnostic and could be done with any Web Server – keesp Dec 21 '18 at 13:39

0 Answers0