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

ServiceWorkerGlobalScope: fetch event

· 52CE 我爱测

The **`fetch`** event of the ServiceWorkerGlobalScope interface is fired in the service worker's global scope when the main app thread makes a network request. It enables the service worker to intercept network requests and send customized responses (for example, from a local cac

Service Workers APIservice

The fetch event of the ServiceWorkerGlobalScope interface is fired in the service worker's global scope when the main app thread makes a network request. It enables the service worker to intercept network requests and send customized responses (for example, from a local cache).

This event is not cancelable and does not bubble.

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("fetch", (event) => { })

Alternatively, assign to the event handler property of the interface:

onfetch = (event) => { }

Event type

A FetchEvent.

Description

The fetch event is fired in the service worker's global scope when the main app thread makes a network request. This includes not only explicit WorkerGlobalScope/fetch calls from the main thread, but also implicit network requests to load pages and subresources (such as JavaScript, CSS, and images) made by the browser following page navigation.

The event handler is passed a FetchEvent object, which provides access to the request as a Request instance.

The FetchEvent also provides a FetchEvent.respondWith() method, that takes a Response (or a Promise that resolves to a Response) as a parameter. This enables the service worker event handler to provide the response that is returned to the request in the main thread.

For example, the service worker can return:

  • A locally cached response retrieved from the Cache interface.
  • A response that the service worker synthesizes, using methods like Response.json() or the Response.Response() constructor.
  • A network error, using the Response.error_static() method. This will cause the fetch() call to reject.

The respondWith() method can only be called once for a given request. If multiple fetch event listeners are added, they will be called in the order they were registered until one of them calls respondWith().

The respondWith() method must be called synchronously: that is, you can't call it in a then handler.

Typically, a fetch event handler will execute different strategies depending on features of the request such as its URL:

function strategy1() {
  return fetch("picnic.jpg");
}

function strategy2() {
  return Response.error();
}

const pattern1 = /^\/salamander/;
const pattern2 = /^\/lizard/;

self.addEventListener("fetch", (event) => {
  const url = new URL(event.request.url);
  if (pattern1.test(url.pathname)) {
    event.respondWith(strategy1());
  } else if (pattern2.test(url.pathname)) {
    event.respondWith(strategy2());
  }
});

If respondWith() is not called in the handler, then the user agent automatically makes the original network request. For example, in the code above, all requests that do not match pattern1 or pattern2 are made as if the service worker did not exist.

Examples

Cache falling back to network

This fetch event handler first tries to find the response in the cache. If a response is found, it returns the cached response. Otherwise, it tries to fetch the resource from the network.

async function cacheThenNetwork(request) {
  const cachedResponse = await caches.match(request);
  if (cachedResponse) {
    console.log("Found response in cache:", cachedResponse);
    return cachedResponse;
  }
  console.log("Falling back to network");
  return fetch(request);
}

self.addEventListener("fetch", (event) => {
  console.log(`Handling fetch event for ${event.request.url}`);
  event.respondWith(cacheThenNetwork(event.request));
});

Cache only

This fetch event handler implements a "cache only" policy for scripts and stylesheets. If the request's Request.destination property is "script" or "style", the handler only looks in the cache, returning an error if the response was not found. All other requests go through to the network.

async function cacheOnly(request) {
  const cachedResponse = await caches.match(request);
  if (cachedResponse) {
    console.log("Found response in cache:", cachedResponse);
    return cachedResponse;
  }
  return Response.error();
}

self.addEventListener("fetch", (event) => {
  if (
    event.request.destination === "script" ||
    event.request.destination === "style"
  ) {
    event.respondWith(cacheOnly(event.request));
  }
});

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

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

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