Table of Contents

Class FetchEvent

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

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

[Value("FetchEvent")]
public class FetchEvent : ExtendableEvent
Inheritance
FetchEvent
Inherited Members

Remarks

Constructors

FetchEvent()

public FetchEvent()

FetchEvent(string, FetchEventInit)

The FetchEvent() constructor creates a new FetchEvent object.

public FetchEvent(string type, FetchEventInit eventInitDict)

Parameters

type string
eventInitDict FetchEventInit

Remarks

Properties

ClientId

The clientId read-only property of the
FetchEvent interface returns the id of the Client that the
current service worker is controlling.

[Value("clientId")]
public string ClientId { get; }

Property Value

string

A string that represents the client ID.

Remarks

The Get(string) method could then be passed this ID to retrieve the
associated client.

-Using Service Workers
-Service workers basic code example
-Using web workers

See also on MDN

Handled

The handled property of the FetchEvent interface returns a promise indicating if the event has been handled by the fetch algorithm or not. This property allows executing code after the browser has consumed a response, and is usually used together with the WaitUntil(Task<dynamic>) method.

[Value("handled")]
public Task<GlobalObject.Undefined> Handled { get; }

Property Value

Task<GlobalObject.Undefined>

A Promise that is pending while the event has not been handled, and fulfilled once it has.

Remarks

PreloadResponse

The preloadResponse read-only property of the FetchEvent interface returns a {{jsxref("Promise")}} that resolves to the navigation preload Response if navigation preload was triggered, or undefined otherwise.

[Value("preloadResponse")]
public Task<dynamic> PreloadResponse { get; }

Property Value

Task<dynamic>

A {{jsxref("Promise")}} that resolves to a Response or otherwise to undefined.

Remarks

Navigation preload is triggered if navigation preload is enabled, the request is a GET request, and the request is a navigation request (generated by the browser when loading pages and iframes).

A service worker can wait on this promise in its fetch event handler in order to track completion of a fetch request made during service-worker boot.

-Speed up Service Worker with Navigation Preloads
-Using Service Workers
-Service workers basic code example
-Using web workers

See also on MDN

ReplacesClientId

The replacesClientId read-only property of the
FetchEvent interface is the Id of the
Client that is being replaced during a page navigation.

[Value("replacesClientId")]
public string ReplacesClientId { get; }

Property Value

string

A string.

Remarks

For example, when navigating from page A to page B replacesClientId is the
ID of the client associated with page A. It can be an empty string when navigating from
about:blank to another page, as about:blank&apos;s client will be
reused, rather than be replaced.

Additionally, if the fetch isn&apos;t a navigation, replacesClientId will be an
empty string. This could be used to access/communicate with a client that will
imminently be replaced, right before a navigation.

-Using Service Workers
-Service workers basic code example
-Using web workers

See also on MDN

Request

The request read-only property of the
FetchEvent interface returns the Request that triggered
the event handler.

[Value("request")]
public Request Request { get; }

Property Value

Request

A Request object.

Remarks

This property is non-nullable (since version 46, in the case of Firefox.) If a request
is not provided by some other means, the constructor options object must
contain a request (see FetchEvent(string, FetchEventInit).)

-Using Service Workers
-Service workers basic code example
-Using web workers

See also on MDN

ResultingClientId

The resultingClientId read-only property of the
FetchEvent interface is the Id of the
Client that replaces the previous client during a page
navigation.

[Value("resultingClientId")]
public string ResultingClientId { get; }

Property Value

string

A string.

Remarks

For example, when navigating from page A to page B resultingClientId is
the ID of the client associated with page B.

If the fetch request is a subresource request or the request&apos;s
destination is
report, resultingClientId will be an empty string.

-Using Service Workers
-Service workers basic code example
-Using web workers

See also on MDN

Methods

RespondWith(Task<Response>)

The respondWith() method of
FetchEvent prevents the browser's default fetch handling, and
allows you to provide a promise for a Response yourself.

[Value("respondWith")]
public GlobalObject.Undefined RespondWith(Task<Response> r)

Parameters

r Task<Response>

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

In most cases you can provide any response that the receiver understands. For example,
if an 'img' initiates the request, the response body needs to be
image data. For security reasons, there are a few global rules:

-Using Service Workers
-Fetch API

See also on MDN