Scope of service-worker?

Asked

Viewed 149 times

0

I am working on a PWA and would like to know if the scope or directory where the service-worker file is located can interfere, such as in push notifications.

A file that is inside /statics/sw.js will work in the same way as one that is at the root of the project??

  • When you register the Voce SW you can set the scope navigator.serviceWorker.register('/statics/sw.js', {scope: '/'})

  • I tried that, and returned an error saying it was above the allowed scope and asked to move the file to the folder /statics and define the scope for /statics also

  • This is the error I return when I set the scope: The path of the provided scope ('/') is not under the max scope allowed ('/statics/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.

1 answer

2


A file inside /statics/sw.js will work the same way as one inside the project root??

Won’t work, it will only have access to fetch events that begin with /statics

Reference:https://developers.google.com/web/fundamentals/primers/service-workers/? hl=en

A subtle point of the Register() method is the location of the service worker file. In this case, you will notice that the service worker file is at the root of the domain. This means that the scope of the service worker will be the full source. In other words, this service worker will receive fetch events for everything in that domain. If we register the service worker file in /example/sw.js, it will only see the fetch events of the URL pages starting with /example/ (i.e., /example/page1/, /example/page2/).

  • And how would I declare a service-worker that will manage my PWA push notifications?? Putting 2 service-Workers at the root of the project, I believe, that gives conflict... Could you provide me some example of how to do?? I really appreciate your help.

  • https://developers.google.com/web/fundamentals/codelabs/push-notifications/? hl=en

  • I solved the problem in an unconventional way, when I gave the command to build the project, the Quasar Framework, which is what I am using generated the service-worker alone and minified. I went to the build settings and I put to not minify and after 'buildado' the project added the listeners I needed in that generated service-worker and worked as expected.

  • Thank you so much for your help and your willingness @David Schrammel...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.