The EdgeWorkers code is invoked based on specific HTTP events.
The responseProvider event handler acts as a surrogate origin to execute
EdgeWorkers code. To learn how more, go to the Response
Orchestration section.
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 requestexportfunction onClientRequest(request) {
var country = request.userLocation.country;
if (country === 'FR') {
request.respondWith(302, {'Location': ['https://www.example.com/fr/']}, '');
}
}
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:
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 originexportfunction onOriginResponse(request, response) {
response.removeHeader('InternalDebugHeader');
}
onClientResponse:
This event happens before the response is sent to the client.
Note: 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: