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');
}
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/' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With