Edge functions
Edge functions (or “serverless functions”) are a way to run custom code on the edge.
This can be used for a variety of purposes such as:
- Checking a request’s geolocation
- Communicating with APIs
- Modifying headers
- Modifying responses
- Modifying requests
Edge functions are written in JavaScript and run on demand.
Getting started
Edge functions are currently in beta and are available to all QuantCDN customers.
To get started, create a new edge function in the QuantCDN dashboard from the “Compute > Functions” section.
The event
object
The event
object is passed to every edge function. It contains information about the request, including the request headers, the request body, and the request URL, etc.
Geolocation
The event.geo
object contains information about the request’s geolocation. A full example of the details returned is shown below:
Some examples of where geolocation information can be used include:
- Display data-collection or cookie consent content that is relevant to the end-user’s country.
- Redirect to a local domain or edition of your website, or offer an option to the user to do so.
- Provide localized stock levels or delivery options.
- Block traffic from regions where you are not licensed to deliver content.
- Block regions that are originating DDoS attacks, or subject requests from those regions to additional checks, such as a CAPTCHA.
- Trigger additional security measures if a user accesses your site from a different country to the one they are normally in.
- Report geolocation data as part of your request logging to better understand your customers.
- Make an API endpoint for your front end code to use to get access to geolocation data in the browser or your client-side app.
IP Address
The true client IP address is exposed in the quant-client-ip
header. This header is added by QuantCDN and contains the original client IP address, and cannot be manipulated.
To retrieve the IP address of a connecting client, check the event.request.headers.get('quant-client-ip')
value.
Headers
The event.request.headers
object contains information about the incoming request’s headers.
Use the .get()
method to get a header value. For example, to get the User-Agent
header, you can use the following:
Use the .set()
method to set a header value. For example, to set a token in the Authorization
header, you can use the following:
Use the .delete()
method to delete a header value. For example, to remove the Accept-Encoding
header, you can use the following:
Header keys are case-insensitive.
Examples
Return basic markup
The most simple example is a function emitting a response with “Hello World” markup with a custom response header.
Returning a JSON response with geolocation data
This example shows how to return a JSON response with basic geolocation data.
Making external HTTP requests
One of the most powerful features of edge functions is the ability to make external HTTP requests. This is useful for a variety of purposes such as:
- Validating user data, such as a JWT or OAuth token
- Fetching data from an API
- Triggering additional security measures, such as a CAPTCHA
- Making an API endpoint for your front end code to use
Edge functions implement the Fetch API for making HTTP requests.
It is better to avoid buffering responses, as the entire body is consumed into memory. Edge functions support streaming responses, so you can stream the response body to the client without buffering if no response manipulation is required.
To compose a request from scratch, use the Request
constructor.