This topic describes event handlers and fetch events of EdgeRoutine (ER).

Register an event

You must call addEventListener in the code of ER to register an event handler. The event handler is called each time a client initiates a request. ER allows multiple event handlers to be called at the same time to process blocked client requests.

If you call an event handler multiple times, a chain of event handlers is formed. For example, event handlers registered by using the following code are called in sequential order. When one of the event handlers calls the respondWith function, the chain of event handlers ends. Event handlers registered after respondWith is called will not be called.
addEventListener('fetch', a);
addEventListener('fetch', b);
Note You can register up to 16 event handlers for a fetch event.

Fetch events

A fetch event occurs when you call the FetchEvent operation to block requests sent to Alibaba Cloud Content Delivery Content (CDN). To listen for fetch events, you must call event.respondWith to register an asynchronous function that returns a Promise object. A Promise object must be resolved into a Response object that is returned to the client request. Otherwise, ER throws exceptions.

The FetchEvent operation is a web API operation. For more information, see FetchEvent. The FetchEvent operation supports the following parameters:
  • waitUntil: ensures that asynchronous tasks are completed even after the Response object is returned.
  • respondWith: responds to the FetchEvent operation.
  • info: a non-standard parameter that represents a JavaScript object. The JavaScript object contains special information, such as the geographic location, client IP address, request scheme, and device information, about the request fetched from Alibaba Cloud CDN.