While extremely lightweight, the push-worker.js file you need to upload is rather instrumental in enabling the logic behind our platform and web push notifications in general. In short, it enables us to listen for incoming push notifications even when someone has left your website. The need for the file to be in the root directory actually stems from the fact that the file must be in the same directory as the page where someone wants to subscribe to notifications (a security measure enforced by Google). That means, if you place it on http://yoursite.com/directory/path, then visitors can only subscribe when visiting http://yoursite.com/directory/path. Having it at the root path allows visitors to subscribe while on any page.That said, while the file itself is required, you can place out outside of the root directory and still allow visitors to subscribe while on any page. It requires a couple extra technical steps but if you have a developer handy they should be able to implement it without any hassle. Here’s a guide on how that can be done:
Specifying the Push Worker File
Overriding the worker file is a bit more complex. The location of the worker file directly impacts which pages on your site can ask for push notifications. This is the reason for requiring it in the root directory by default.
If you can’t add it to the root directory, you need to do two steps:
- Update the worker file to pass a custom header: “Service-Worker-Allowed: /”
For instance, if you are using PHP you could create a file called updated-push-worker.js.php with the following:
- Reference the newly created file in your tracking code snippet.
After creating the above file, simply declare it using:
_at.worker = ‘/another/path/updated-push-worker.js.php’;
- So it would look like: