From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- files/ko/web/api/fetchevent/index.html | 110 +++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 files/ko/web/api/fetchevent/index.html (limited to 'files/ko/web/api/fetchevent/index.html') diff --git a/files/ko/web/api/fetchevent/index.html b/files/ko/web/api/fetchevent/index.html new file mode 100644 index 0000000000..9b0e1cce34 --- /dev/null +++ b/files/ko/web/api/fetchevent/index.html @@ -0,0 +1,110 @@ +--- +title: FetchEvent +slug: Web/API/FetchEvent +tags: + - API + - FetchEvent + - Interface + - NeedsTranslation + - Offline + - Reference + - Service Workers + - TopicStub + - Workers +translation_of: Web/API/FetchEvent +--- +

{{SeeCompatTable}}{{APIRef("Service Workers API")}}

+ +

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

+ +

Constructor

+ +
+
{{domxref("FetchEvent.FetchEvent()", "FetchEvent()")}}
+
Creates a new FetchEvent object. This constructor is not typically used. The browser creates these objects itself and provides them to fetch event callbacks.
+
+ +

Properties

+ +

Inherits properties from its ancestor, {{domxref("Event")}}.

+ +
+
{{domxref("fetchEvent.clientId")}} {{readonlyInline}}
+
The {{domxref("Client.id", "id")}} of the same-origin {{domxref("Client", "client")}} that initiated the fetch.
+
{{domxref("fetchEvent.preloadResponse")}} {{readonlyinline}}
+
A {{jsxref("Promise")}} for a {{domxref("Response")}}, or void if this is not a navigation, or {{domxref("NavigationPreloadManager", "navigation preload", "", 1)}} is not enabled.
+
{{domxref("fetchEvent.request")}} {{readonlyInline}}
+
The {{domxref("Request")}} the browser intends to make.
+
+ +

Methods

+ +

Inherits methods from its parent, {{domxref("ExtendableEvent")}}.

+ +
+
{{domxref("fetchEvent.respondWith()")}}
+
Prevent the browser's default fetch handling, and provide (a promise for) a response yourself.
+
{{domxref("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.

+
+
+ +

Examples

+ +

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.

+ +
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);
+  }());
+});
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#fetch-event-section', 'FetchEvent')}}{{Spec2('Service Workers')}}Initial definition.
+ +

Browser compatibility

+ +
+ + +

{{Compat("api.FetchEvent")}}

+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf