大网数据广告活动
Web 标准与前端开发

Service-Worker-Allowed header

· 52CE 我爱测

The HTTP **`Service-Worker-Allowed`** response header is used to broaden the path restriction for a service worker's default `scope`.

The HTTP Service-Worker-Allowed response header is used to broaden the path restriction for a service worker's default scope.

By default, the scope for a service worker registration is the directory where the service worker script is located. For example, if the script sw.js is located in /js/sw.js, it can only control URLs under /js/ by default. Servers can use the Service-Worker-Allowed header to allow a service worker to control URLs outside of its own directory.

A service worker intercepts all network requests within its scope, so you should avoid using overly-broad scopes unless necessary.

<table class="properties"> <tbody> <tr> <th scope="row">Header type</th> <td>Response header</td> </tr> </tbody> </table>

Syntax

Service-Worker-Allowed: <scope>

Directives

  • <scope>
    • : A string representing a URL that defines a service worker's registration scope; that is, what range of URLs a service worker can control.

Examples

Using Service-Worker-Allowed to broaden service worker scope

The JavaScript example below is included in example.com/product/index.html, and attempts to register a service worker with a scope that applies to all resources under example.com/.

navigator.serviceWorker.register("./sw.js", { scope: "/" }).then(
  (registration) => {
    console.log("Install succeeded, scoped to '/'", registration);
  },
  (error) => {
    console.error(`Service worker registration failed: ${error}`);
  },
);

The HTTP response to the service worker's script resource request (./sw.js) includes the Service-Worker-Allowed header set to /:

HTTP/1.1 200 OK
Date: Mon, 16 Dec 2024 14:37:20 GMT
Service-Worker-Allowed: /

// sw.js contents…

If the server doesn't set the header, the service worker registration will fail, as the scope option ({ scope: "/" }) requests a scope broader than the directory where the service worker script is located (/product/sw.js).

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。