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?
805
06-Aug-2025
Updated on 19-Aug-2025
ICSM Computer
18-Aug-2025Scope 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().