FetchEvent

This is the event type for fetch events dispatched on the service worker global scope . It contains information about the fetch, including the request and how the receiver will treat the response. It provides the event.respondWith() method, which allows us to provide a response to this fetch.

构造函数

FetchEvent()
创建新的 FetchEvent object. This constructor is not typically used. The browser creates these objects itself and provides them to fetch event callbacks.

特性

Inherits properties from its ancestor, Event .

FetchEvent.clientId 只读
id of the same-origin client that initiated the fetch.
FetchEvent.preloadResponse 只读
A Promise for a Response ,或 undefined if this fetch is not a navigation, or navigation preload is not enabled.
FetchEvent.replacesClientId 只读
id of the client that is being replaced during a page navigation.
FetchEvent.resultingClientId 只读
id of the client that replaces the previous client during a page navigation.
FetchEvent.request 只读
Request the browser intends to make.

方法

Inherits methods from its parent, ExtendableEvent .

FetchEvent.respondWith()
Prevent the browser's default fetch handling, and provide (a promise for) a response yourself.
ExtendableEvent.waitUntil()

Extends the lifetime of the event. Used to notify the browser of tasks that extend beyond the returning of a response, such as streaming and caching.

范例

This fetch event uses the browser default for non-GET requests. For GET requests it tries to return a match in the cache, and falls back to the network. If it finds a match in the cache, it asynchronously updates the cache for next time.

self.addEventListener('fetch', event => {
  // Let the browser do its default thing
  // for non-GET requests.
  if (event.request.method != 'GET') return;
  // Prevent the default, and handle the request ourselves.
  event.respondWith(async function() {
    // Try to get the response from a cache.
    const cache = await caches.open('dynamic-v1');
    const cachedResponse = await cache.match(event.request);
    if (cachedResponse) {
      // If we found a match in the cache, return it, but also
      // update the entry in the cache in the background.
      event.waitUntil(cache.add(event.request));
      return cachedResponse;
    }
    // If we didn't find a match in the cache, use the network.
    return fetch(event.request);
  }());
});
					

规范

规范 状态 Comment
服务工作者
The definition of 'FetchEvent()' in that specification.
工作草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
FetchEvent
Chrome 40 Edge ≤18 Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 不支持 No WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 不支持 No Samsung Internet Android 4.0
FetchEvent() 构造函数
Chrome 40 Edge ≤18 Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 不支持 No WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 不支持 No Samsung Internet Android 4.0
client
弃用 非标
Chrome 42 Edge ≤79 Firefox 44 IE 不支持 No Opera 27 Safari 不支持 No WebView Android 42 Chrome Android 44 Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android 4.0
clientId
Chrome 49 Edge ≤79 Firefox 45
注意事项
45
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 36 Safari 不支持 No WebView Android 49 Chrome Android 49 Firefox Android 45 Opera Android 36 Safari iOS 不支持 No Samsung Internet Android 5.0
isReload
弃用
Chrome 45 Edge 17 Firefox 不支持 44 — 74
注意事项
44 — 74
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 32 Safari 不支持 No WebView Android 45 Chrome Android 45 Firefox Android 44 Opera Android 32 Safari iOS 不支持 No Samsung Internet Android 5.0
navigationPreload
Chrome 59 Edge ≤79 Firefox ? IE 不支持 No Opera 46 Safari 不支持 No WebView Android 59 Chrome Android 59 Firefox Android ? Opera Android 43 Safari iOS 不支持 No Samsung Internet Android 7.0
preloadResponse
Chrome 59 Edge 18 Firefox ? IE 不支持 No Opera 46 Safari 不支持 No WebView Android 59 Chrome Android 59 Firefox Android ? Opera Android 43 Safari iOS 不支持 No Samsung Internet Android 7.0
replacesClientId Chrome 不支持 No Edge 不支持 18 — 79 Firefox 65 IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 65 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
request
Chrome Yes Edge ≤79 Firefox 44 IE 不支持 No Opera Yes Safari 不支持 No WebView Android Yes Chrome Android Yes Firefox Android ? Opera Android Yes Safari iOS 不支持 No Samsung Internet Android Yes
respondWith
Chrome 42
注意事项
42
注意事项
NetworkError thrown if request mode is same-origin and response type is cors (see bug 1222008 ). This is being worked on - see https://www.chromestatus.com/feature/5694278818856960 .
Edge ≤79
注意事项
≤79
注意事项
NetworkError thrown if request mode is same-origin and response type is cors (see bug 1222008 ). This is being worked on - see https://www.chromestatus.com/feature/5694278818856960 .
Firefox 59
注意事项
59
注意事项
NetworkError thrown if request mode is same-origin and response type is cors (see bug 1222008 ).
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 42
注意事项
42
注意事项
NetworkError thrown if request mode is same-origin and response type is cors (see bug 1222008 ). This is being worked on - see https://www.chromestatus.com/feature/5694278818856960 .
Chrome Android 42
注意事项
42
注意事项
NetworkError thrown if request mode is same-origin and response type is cors (see bug 1222008 ). This is being worked on - see https://www.chromestatus.com/feature/5694278818856960 .
Firefox Android ? Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0
resultingClientId Chrome 72 Edge 18 Firefox 65 IE 不支持 No Opera 60 Safari 不支持 No WebView Android 72 Chrome Android 72 Firefox Android 65 Opera Android 50 Safari iOS 不支持 No Samsung Internet Android 11.0
targetClientId Chrome ? Edge ? Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android ? Chrome Android ? Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android ?

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。

另请参阅