Product
Solutions
Developers
Demo
Pricing
Why EdgeOne
Limited Time Free!
Enjoy
14-day trial plus free 100GB
for website acceleration and advanced security protection!
Get Started Now
Edge Functions
Some example code snippets with Edge Functions implementation.
Examples
Returning an HTML Page
In this example, an edge function is used to generate an HTML page, and the HTML page is accessed and previewed from a browser.
Returning a JSON Object
In this example, an edge function is used to generate a JSON object, and the JSON object is accessed and previewed from a browser.
Fetch Remote Resources
In this example, the Fetch API is called to fetch a remote jQuery.js resource and send the resource to a client in response to a request from the client.
Authenticating a Request Header
This example demonstrates how to use an edge function to perform simple permission control by verifying the value of the x-custom-token request header.
Modifying a Response Header
This example uses the Fetch API to implement a reverse proxy for the domain name www.example.com of the site, and sets the HTTP response headers through Edge functions to achieve CORS (Cross-Origin Resource Sharing).
Performing an A/B Test
In this example, cookies are used to store session information and perform A/B testing on requests. This example demonstrates how to use an edge function to perform A/B testing.
Setting Cookies
In this example, cookies are used to count the number of access requests. When a browser accesses the Edge Functions service, the number of access requests is increased by 1.
Performing Redirect Based on the Request Location
This example implements automatic redirection to the Target url of the region belonging to the Client by judging the region of the Client. It realizes the delivery of requests based on the region of the Client through Edge functions.
Using the Cache API
In this sample edge function, the Fetch API is called to fetch a remote jQuery.js resource, and the Cache API is called to cache the resource to an EdgeOne edge node. The cache duration is set to 10s.
Caching POST Requests
In this example, an SHA-256 signature is calculated for the request body of a POST request and used as a part of the cache key, and the Cache API is called to cache the response content. If the content is already stored in the cache, the cached content is sent to the client.
Responding in Streaming Mode
In this example, the Fetch API is called to fetch a remote jQuery.js resource, and the resource is sent to a client in streaming mode in response to a request from the client.
Merging Resources and Responding in Streaming Mode
In this example, three video clips are merged into one video, and the merged video is played on a client based on the order in which the video clips are merged.
Protecting Data from Tampering
In this example, an SHA-256 signature is calculated for a request body and compared with the signature that is generated by the origin server. If the signatures are the same, the response content is not tampered with.
Rewriting a m3u8 File and Configuring Authentication
In this example, a .m3u8 file is rewritten to support TypeA authentication that controls access to .m3u8 files and .ts segments. You can modify the code as needed to support other authentication methods.
Adaptive Image Format Conversion
In this document, we demonstrate how to identify the browser type via User-Agent in the request header, use fetch API to get the image from the origin, and convert it to the format required by the browser.
Adaptive Image Resize
In this document, we demonstrate how to identify the client type via User-Agent in the request header, use fetch API to get the image from the origin, and convert it to the format required by the browser.
Image Adaptive WebP
This example determines whether the request header Accept contains image/webp. If so, the Edge function will automatically convert the image format to WebP and cache it on the EdgeOne edge node.
Custom Referer Anti-leeching
This example determines the request sources by checking the Referer field in the HTTP request header. You can flexibly customize the matching rules for this Referer.
Remote Authentication
In order to avoid customers' resources being accessed by illegal users, this example transmits the request to the customer-specified remote authentication server.
HMAC Digital Signature
Web Crypto API is used in this example to achieve HMAC-SHA256 signature and store the signature information in a request header.
Naming a Downloaded File
In this example, the modification of the Content-Disposition in the response headers achieves the modification of the downloaded file name according to the fileName parameter in the request URL.
Obtaining Client IP Address
The client IP address is obtained in this example through client IP header EO-Client-IP activated in the rule engine, and assembled into data in the form of JSON to respond to the clients, which succeeds in obtaining client IP address by the use of edge function.