I am trying to implement a Service Worker
in a test page. My end goal is an application that operates offline. The folder structure is below
/myApp
...
/static
/mod
/practice
service-worker.js
worker-directives.js
foopage.js
/templates
/practice
foopage.html
I am registering a service worker as shown below (within service-worker.js
):
navigator.serviceWorker.register('../static/mod/practice/service-worker.js').then(function(reg) {
console.log('Registration succeeded. Scope is ' + reg.scope);
...
}
and in the console I see
Registration succeeded. Scope is https://example.com/static/mod/practice/
If my page is located at https://example.com/practice/foopage
, do I need to make sure that my service worker
scope is https://example.com/practice/foopage
?
If I try to define the scope in the register
function call like
navigator.serviceWorker.register('../static/mod/practice/service-worker.js', { scope: '/practice/foopage/' }).then(function(reg) {
...
}
I get the error
Registration failed with SecurityError: Failed to register a ServiceWorker: The path of the provided scope ('/practice/foopage/') is not under the max scope allowed ('/static/mod/practice/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.
Question is: What exactly does scope refer to? Is it the collection of URLs that the service worker
will eventually control? Do I need to move service-workers.js
somewhere else? If so, where?
v15.2.0
. – Unfortunate