Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PWA - service-worker JS in subdirectory

I tried to have my sw.js file in a subdirectory instead of the root directory. But then I get an error that says the service-worker could not be installed.

Is it really necessary to have this file in the root-directory or am I just missing something?

JS-Code:

if ('serviceWorker' in navigator) {

    navigator.serviceWorker.register('/js/pwa/sw.js', { scope: '/js/pwa' }).then(function(reg) {
        console.log('Successfully registered. Scope is ' + reg.scope);
    }).catch(function(error) {
        console.log('Registering failed ' + error);
    });
} else {
    console.log('Service worker can not be registered on this device');
}
like image 474
Swissdude Avatar asked Oct 17 '25 02:10

Swissdude


1 Answers

If I did the same thing, I got the following error

Registering failed SecurityError: Failed to register a ServiceWorker: The path of the provided scope ('/js/pwa') is not under the max scope allowed ('/js/pwa/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.

The following parts seemed strange

{ scope: '/js/pwa' }

Please do as follows

{ scope: '/js/pwa/' }
like image 115
sachiko-kame Avatar answered Oct 19 '25 00:10

sachiko-kame



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!