What is the scope of a service worker, and how can it be controlled?
What is the scope of a service worker, and how can it be controlled?
Student
The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
Scope of a Service Worker
The scope of a service worker defines the set of pages (URLs) and requests it can control.
For example:
/sw.js, it will control/and everything under it./scripts/sw.js, it will control/scripts/and all subpaths.How to Control Scope
When registering the service worker in JavaScript, you can explicitly set its scope using the
scopeoption:Here:
/sw.jsis the service worker file.scope: '/app/'means it will only control URLs starting with/app/.Rules & Limitations
scope, it defaults to the worker file’s directory and below.Example: If
sw.jsis at/app/sw.js, it cannot control/(the root).Summary:
scopeoption inregister().