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:
{ "as_name": "amazon.com inc.", "as_number": 16509, "area_code": 0, "city": "tokyo", "conn_speed": "broadband", "conn_type": "wired", "continent": "AS", "country_code": "JP", "country_code3": "JPN", "country_name": "japan", "latitude": 35.68, "longitude": 139.75, "metro_code": 392001, "postal_code": "100-0001", "proxy_description": "cloud", "proxy_type": "hosting", "region": "13", "utc_offset": 900}
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:
const userAgent = event.request.headers.get('User-Agent');
Use the .set()
method to set a header value. For example, to set a token in the Authorization
header, you can use the following:
event.request.headers.set('Authorization', 'Bearer 1234567890');
Use the .delete()
method to delete a header value. For example, to remove the Accept-Encoding
header, you can use the following:
event.request.headers.delete('Accept-Encoding');
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.
const headers = new Headers({ 'Content-Type': 'text/html;' 'X-Powered-By': 'QuantCDN'})
const markup = ` <!DOCTYPE html> <html> <head> <title>Hello World</title> </head> <body> <h1>Hello World, from Quant Edge Functions</h1> </body> </html>`;
return new Response(markup, { status: 200, headers: headers });