fetch
event listeners.fetch
event listener to generate an HTTP request event FetchEvent, thereby processing HTTP requests.FetchEvent
object cannot be constructed directly. You can use addEventListener
to register a fetch
event listener to obtain the event
object.// `event` is the `FetchEvent` object.addEventListener('fetch', (event) => {event.respondWith(new Response('hello world!'));});
// event.clientIdreadonly clientId: string;
// event.requestreadonly request: Request;
event.respondWith(response: Response | Promise<Response>): void;
fetch
event callback of the addEventListener
event listener, the event.respondWith()
method is used to respond to the client. If this method is not invoked, Edge Functions forwards the current request back to the origin.event.waitUntil(task: Promise<any>): void;
Promise
-based task is completed, extending the event processing lifecycle.Parameter | Type | Required | Description |
task | Yes | The `Promise`-based task. |
event.passThroughOnException(): void;
event.respondWith
method is not invoked, Edge Functions forwards the current request back to the origin.function handleRequest(request) {return new Response('Edge Functions, Hello World!');}addEventListener('fetch', event => {const request = event.request;// If the request URL contains the string /ignore/, Edge Functions forwards the current request back to the origin.if (request.url.indexOf('/ignore/') !== -1) {// The event.respondWith method is not invoked.return;}// Customize content in the edge function to respond to the client.event.respondWith(handleRequest(request));});
addEventListener('fetch', event => {// If the function code throws an unhandled exception, Edge Functions forwards the current request back to the origin.event.passThroughOnException();throw new Error('Throw error');});
Was this page helpful?