---
title: "What is the scope of a service worker, and how can it be controlled?"  
description: "What is the scope of a service worker, and how can it be controlled?"  
author: "Anubhav Sharma"  
published: 2025-08-06  
updated: 2025-08-19  
canonical: https://www.mindstick.com/forum/161853/what-is-the-scope-of-a-service-worker-and-how-can-it-be-controlled  
category: "javascript"  
tags: ["javascript", "service"]  
reading_time: 2 minutes  

---

# What is the scope of a service worker, and how can it be controlled?

**What is the [scope](https://www.mindstick.com/interview/372/what-are-the-different-scopes-for-java-variables) of a service worker, and how can it be [controlled](https://yourviews.mindstick.com/view/81837/has-pakistan-controlled-corona-pandemic)?**

## Replies

### Reply by ICSM Computer

### Scope of a [Service Worker](https://www.mindstick.com/interview/34350/how-do-you-register-a-service-worker-in-a-web-application)

The **scope** of a service worker defines the set of pages (URLs) and requests it can **control**.

- By default, the scope is limited to the **directory of the service worker file** and all subdirectories.\ For example:

   - If you register `/sw.js`, it will control `/` and everything under it.
   - If you register `/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 `scope` option:

```javascript
navigator.serviceWorker.register('/sw.js', {
  scope: '/app/'
}).then(function(registration) {
  console.log('Service Worker registered with scope:', registration.scope);
});
```

Here:

- `/sw.js` is the service worker file.
- `scope: '/app/'` means it will only control URLs starting with `/app/`.

### Rules & Limitations

1. **Same-origin rule**: The scope must be within the same origin as the service worker file.

   1. You can’t register a service worker to control another domain or higher-level path than where it is served from.

2. **Default scope**: If you don’t specify `scope`, it defaults to the worker file’s directory and below.
3. **Max scope**: The scope **cannot be broader than the service worker file’s path**.\ Example: If `sw.js` is at `/app/sw.js`, it cannot control `/` (the root).
4. **Multiple workers**: Different service workers can be registered for different scopes on the same origin.

## Summary:

- Scope = which URLs the [service worker controls](https://www.mindstick.com/forum/161853/what-is-the-scope-of-a-service-worker-and-how-can-it-be-controlled).
- Default = its directory + subdirectories.
- Controlled by `scope` option in `register()`.
- Restricted by **origin** and **path rules**.


---

Original Source: https://www.mindstick.com/forum/161853/what-is-the-scope-of-a-service-worker-and-how-can-it-be-controlled

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
