DEV Community

André
André

Posted on • Updated on

Configure your Apache server to allow Service Workers

Trying out Service Workers in your web project, you might have encountered this error message at some point:

The path of the provided scope ('/') is not under the max scope allowed ('/assets/js/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.

This happens when you try to place your Service Worker (let's call it sw.js) in some folder other than the root folder of your project. While it's basically okay to put that file at root, it might clash with the pattern you had in mind for your project and you'd rather have sw.js in a folder like /assets/js/.

Well, the error message already has all the hints you need to achieve this.

In order to modify the HTTP header, you'll first have to load the headers module in your httpd.conf file by adding/uncommenting this line:
LoadModule headers_module modules/mod_headers.so

Then, assuming you're serving via HTTPS, go into your httpd-ssl.conf file and add the following lines:

<IfModule mod_headers.c>
<Files "sw.js">
Header set Service Worker-Allowed "/"
</Files>
</IfModule>
Enter fullscreen mode Exit fullscreen mode

And that's it.
You should now be able to place your Service Worker at basically any place you like inside your project.

Top comments (1)

Collapse
 
regdev profile image
Régis • Edited

Thx !

For my part, I had to add the following syntax to make it work on Apache 2.4 :

Header always set Service-Worker-Allowed "/"