This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
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
Read only
id
of the same-origin client
that initiated the fetch.fetchEvent.preloadResponse
Read only
Promise
for a Response
, or void if this is not a navigation, or navigation preload is not enabled.fetchEvent.request
Read only
Request
the browser intends to make.fetchEvent.replacesClientId
Read only
fetchEvent.resultingClientId
Read only
Inherits methods from its parent, ExtendableEvent
.
fetchEvent.respondWith()
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); }()); });
Specification | Status | Comment |
---|---|---|
Service Workers The definition of 'FetchEvent()' in that specification. | Working Draft | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 40 | Yes | 44
|
No | 27 | No |
FetchEvent() constructor
|
40 | Yes | 44
|
No | 27 | No |
client
|
42 | ? | 44 | No | 24 | No |
clientId
|
49 | ? | 45
|
No | 36 | No |
isReload
|
45 | 17 | 44
|
No | 32 | No |
navigationPreload
|
59 | ? | ? | ? | 46 | ? |
preloadResponse
|
59 | 18 | ? | ? | 46 | ? |
request
|
Yes | ? | 44 | No | Yes | No |
respondWith
|
42
|
? | 59
|
No | 29 | No |
resultingClientId |
? | 18 | ? | ? | ? | ? |
targetClientId |
? | ? | ? | ? | ? | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 40 | 40 | ? | 44 | 27 | No | 4.0 |
FetchEvent() constructor
|
40 | 40 | ? | 44 | 27 | No | 4.0 |
client
|
? | 44 | ? | No | ? | No | 4.0 |
clientId
|
49 | 49 | ? | 45 | 36 | No | 5.0 |
isReload
|
45 | 45 | 17 | 44 | 32 | No | 5.0 |
navigationPreload
|
59 | 59 | ? | ? | 46 | ? | 7.0 |
preloadResponse
|
59 | 59 | ? | ? | 46 | ? | 7.0 |
request
|
? | Yes | ? | ? | Yes | ? | Yes |
respondWith
|
42
|
42
|
? | ? | 29 | ? | 4.0 |
resultingClientId |
? | ? | ? | ? | ? | ? | ? |
targetClientId |
? | ? | ? | ? | ? | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent