EdgeWorkers

Learn how to execute JavaScript functions at the edge to optimize site performance and customize web experiences.

Get Started    

Event handler functions

Understand when EdgeWorker scripts are executed. The EdgeWorkers code is invoked based on specific HTTP events.

IMAGE_STUBIMAGE_STUB

The responseProvider event handler acts as a surrogate origin to execute EdgeWorkers code. To learn how more, go to the Response Orchestration section.

responseProvider event handlerresponseProvider event handler

  1. onClientRequest: This event happens for every request as the request is received, before checking if a response is available in cache. Use this event for request modifications before going to cache or to origin. Here's an example of a function that modifies the response based on user location:
// redirect based on originating country of request
 
export function onClientRequest(request) {
   var country = request.userLocation.country;
    if (country === 'FR') {
       request.respondWith(302, {'Location': ['https://www.example.com/fr/']}, '');
    }
}
  1. onOriginRequest: This happens just before sending the request to the origin. This event only happens if the response is not served from cache and not constructed on the edge. Use this event if you want to affect the response coming back from the origin. This function adds a device type header to the origin request:
// send header to origin
 
export function onOriginRequest(request) {
   if (request.device.isMobile) { 
      request.setHeader('X-MyHeader-DeviceType','Mobile'); 
   } 
   else if (request.device.isTablet) { 
      request.setHeader('X-MyHeader-DeviceType', 'Tablet'); 
   } 
   else { 
      request.setHeader('X-MyHeader-DeviceType', 'Desktop'); 
   }
}
  1. onOriginResponse: This event happens as the origin response is created. The event only happens if the response is not served from cache and not constructed on the edge. Use this event if you want to modify the response before it is cached.
// remove header returned from origin
 
export function onOriginResponse(request, response) {
response.removeHeader('InternalDebugHeader');
}
  1. onClientResponse: This event happens before the response is sent to the client.

📘

You cannot currently modify the response body. You can only modify the response headers.

This example shows how to set a header before the response is sent to the client:

// add response header
 
export function onClientResponse(request, response) {
response.addHeader('Powered-By','Akamai EdgeWorkers');
}
  1. responseProvider: This event happens if the response is not already found in cache. This example shows how to return a response body:
//add content to the response

export function responseProvider(request) {
return Promise.resolve(createResponse(200, {}, "<html><head></head><body><p>Hello World</p></body></html>"));
}

📘

To find more information about how EdgeWorker scripts are executed during the responseProvider event see Response Orchestration.

To create a function for any of these events, implement the callbacks as explained in the JavaScript interface reference .

Event handler methods

View the supported HTTP methods for each of the EdgeWorkers event handlers.

Event handler

Supported methods

Unsupported methods

onOriginRequest
onOriginResponse
onClientResponse

GET
HEAD
POST
PUT
DELETE

CONNECT
TRACE
PATCH
OPTIONS

onClientRequest

GET
HEAD
OPTIONS

CONNECT
POST
PUT
DELETE
TRACE
PATCH

responseProvider

GET
HEAD

POST
PUT
DELETE
CONNECT
TRACE
PATCH
OPTIONS

Bypass event handlers

To prevent specific event handlers from executing you can create a Property Manager variable.

Refer to this table for the PMUSER variable name to use for each event handler.

Event handler

Variable name

onClientRequest

BYPASS_EW_CLTREQ_EVENT

onOriginRequest

BYPASS_EW_ORGNREQ_EVENT

onOriginResponse

BYPASS_EW_ORGNRESP_EVENT

onClientResponse

BYPASS_EW_CLTRESP_EVENT

responseProvider

BYPASS_EW_RP_EVENT

To bypass an event, set the initial value of the corresponding variable to "true".

👍

The PMUSER_ prefix for the variable is automatically included.

Updated about a month ago


Event handler functions


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.