FetchEvent

この翻訳は不完全です。英語から この記事を翻訳 してください。

これは実験段階の機能です。
この機能は複数のブラウザで開発中の状態にあります。各ブラウザで用いるために、適切なベンダー接頭辞が必要な場合があります。互換性テーブルをチェックしてください。また、実験段階の機能の構文と挙動は、仕様変更に伴い各ブラウザの将来のバージョンで変更になる可能性があることに注意してください。

The parameter passed into the ServiceWorkerGlobalScope.onfetch handler, FetchEvent represents a fetch action that is dispatched on the ServiceWorkerGlobalScope of a ServiceWorker. It contains information about the request and resulting response, and provides the FetchEvent.respondWith() method, which allows us to provide an arbitrary response back to the controlled page.

Constructor

FetchEvent.FetchEvent()
Creates a new FetchEvent object.

プロパティ

Inherits properties from its ancestor, Event.

FetchEvent.isReload 読取専用
Returns a Boolean that is true if the event was dispatched with the user's intention for the page to reload, and false otherwise. Typically, pressing the refresh button in a browser is a reload, while clicking a link and pressing the back button is not.
FetchEvent.request 読取専用
Returns the Request that triggered the event handler.
FetchEvent.clientId 読取専用
Returns the id of the client that the current service worker is controlling.

非推奨のプロパティ

FetchEvent.client 読取専用
Returns the Client that the current service worker is controlling.

メソッド

Inherits methods from its parent, ExtendableEvent.

FetchEvent.respondWith()
Resolves by returning a Response or a network error  to Fetch.
ExtendableEvent.waitUntil()

Extends the lifetime of the event.  It is intended to be called in the install EventHandler for the installing worker and on the active EventHandler for the active worker.

This code snippet is from the service worker fetch sample (run the fetch sample live). In an earlier part of the code,  an InstallEvent controls caching of a number of resources. The ServiceWorkerGlobalScope.onfetch event handler then listens for the fetch event. When fired, FetchEvent.respondWith() returns a promise back to the controlled page. This promise resolves to the first matching URL request in the Cache object. If no match is found (i.e. that resource wasn't cached in the install phase), the code fetches a response from the network.

The code also handles exceptions thrown from the ServiceWorkerGlobalScope.fetch() operation. Note that a HTTP error response (e.g., 404) doesn't trigger an exception. It returns a normal response object that has the appropriate error code set.

self.addEventListener('fetch', function(event) {
  console.log('Handling fetch event for', event.request.url);

  event.respondWith(
    caches.match(event.request).then(function(response) {
      if (response) {
        console.log('Found response in cache:', response);

        return response;
      }
      console.log('No response found in cache. About to fetch from network...');

      return fetch(event.request).then(function(response) {
        console.log('Response from network is:', response);

        return response;
      }).catch(function(error) {
        console.error('Fetching failed:', error);

        throw error;
      });
    })
  );
});

Specifications

Specification Status Comment
Service Workers
The definition of 'FetchEvent' in that specification.
草案 Initial definition.

ブラウザ互換性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 40.0 44.0 (44.0) 未サポート 24 未サポート
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support 未サポート ? 44.0 (44.0) (有) 未サポート ? 未サポート 44.0

関連項目

ドキュメントのタグと貢献者

 このページの貢献者: lv7777
 最終更新者: lv7777,