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,
事件
.
FetchEvent.clientId
只读
id
of the same-origin
client
that initiated the fetch.
FetchEvent.preloadResponse
只读
Promise
对于
响应
,或
undefined
if this fetch is not a navigation, or
navigation preload
is not enabled.
FetchEvent.replacesClientId
只读
id
的
client
that is being replaced during a page navigation.
FetchEvent.resultingClientId
只读
id
的
client
that replaces the previous client during a page navigation.
FetchEvent.request
只读
Request
the browser intends to make.
继承方法来自其父级
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);
}());
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
服务工作者
The definition of 'FetchEvent()' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
FetchEvent
|
Chrome 40 | Edge ≤18 |
Firefox
44
注意事项
|
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
注意事项
|
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
注意事项
|
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
注意事项
|
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
注意事项
|
Edge
≤79
注意事项
|
Firefox
59
注意事项
|
IE 不支持 No | Opera 29 | Safari 不支持 No |
WebView Android
42
注意事项
|
Chrome Android
42
注意事项
|
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 ? |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
FetchEvent
缓存
CacheStorage
Client
Clients
ExtendableEvent
InstallEvent
Navigator.serviceWorker
NotificationEvent
PeriodicSyncEvent
PeriodicSyncManager
PeriodicSyncRegistration
ServiceWorker
ServiceWorkerContainer
ServiceWorkerGlobalScope
ServiceWorkerRegistration
SyncEvent
SyncManager
SyncRegistration
WindowClient