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?
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?
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.