1

While implementing web push notifications, I have to add a service-worker file location in my JS code.

navigator.serviceWorker.register('/service-worker.js')

Can I host this service-worker file in a CDN or Google Drive?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Ravikrn
  • 387
  • 1
  • 3
  • 19
  • 2
    can you please post what you tried so far ? – user1140237 Dec 28 '15 at 08:20
  • @user1140237, I tried to host service-worker file on google-drive and add path in while registering service worker. But, its showing 404 error, its only considering paths which are extensions of root. – Ravikrn Dec 28 '15 at 09:03
  • [Google Drive hosting is being removed.](http://googleappsupdates.blogspot.com/2015/08/deprecating-web-hosting-support-in.html) – Daniel Herr Dec 28 '15 at 18:45
  • What about cdn hosting? Can I host my service-worker on cdn? – Ravikrn Dec 28 '15 at 18:52

1 Answers1

4

Your service worker JavaScript file, the one that gets passed in to navigator.serviceWorker.register(), can't live on a different domain that the pages it controls. If your site is served from https://www.example.com/, then you can't serve your service worker JavaScript file from https://www.somecdn.com/. You'd have to make sure all of your web pages and your service worker file are all served from the same domain, though whether that single domain corresponds to a CDN's server or something else is not important.

Additionally, the path to the service worker script file affects the maximum scope of the service worker's control, and you'll generally need to place the service worker script file at the "top level" of your website if you want to control all the pages on the site. There's more information in this other Stack Overflow response.

Community
  • 1
  • 1
Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167