Products
Edge Acceleration
CDN
Power your content delivery with speed, security and reliability
Smart Acceleration
Accelerate dynamic content via intelligent routing
L4 Proxy
Support TCP/UDP acceleration
Edge Security
DDoS Protection
Mitigate DDoS attacks in real time
Bot Management
Protect your platforms with intelligent bot mitigation
Web Protection
Safeguard your websites and apps
CAPTCHA
Block automated attacks with multi-layered CAPTCHA
Edge Developer Platform
Pages
Launch your web app at lightning speed
Edge Functions
Deploy serverless code globally across edge networks
Image Renderer
Generate images at the edge
Edge Media
VOD
Try demo
Optimize video delivery with AI-powered transcoding
Solutions
Industry
Gaming
E-commerce & Retail
Media & Entertainment
Financial Services
Use Case
Global Expansion to China
Empowers "Reverse: 1999" Launch with EdgeOne's Security and Acceleration
All Success Stories
Developers
Documentation
Learning Center
API
Get started
Site Acceleration
Security Protection
Pages
Edge Functions
L4 Proxy Service
PLans & pricing
Free Plan
Personal Plan
Basic Plan
Add-Ons
TECH SUPPORT
Github
Discord
Telegram
WhatsApp
Developer Hub
Learn More
A platform for developers to collaborate and create.
Resources
ENGAGE
Learning Center
Resources on cyber security and how the Internet works from EdgeOne
Blog
Deep dives into EdgeOne's tech and product updates
Topic
EdgeOne’s proven expertise in CDN and security
VOD Demo
See how EdgeOne optimizes video delivery with Al-powered transcoding
Tools
Online developer utilities for web, network, and media optimization
BUILD
Documentation
Learn how to accelerate, secure, and build at the edge with EdgeOne
Tutorial
Step-by-step guides to quickly implement EdgeOne's capabilities
Pricing
PURCHASE OPTIONS
Plans & Pricing
ADD-ONS
PRICING GUIDANCE
Pricing Documents
Company
ABOUT US
Why Edgeone
Network Map
RESOURCE
Success Stories
Reports
Events
PARTNER
Channel Partner
Peering Portal
SOCIAL MEDIA
X
YouTube
Linkedin
🎉 EdgeOne Free Plan Launches! The World's First Free CDN with China Access – Join the Event to Unlock Multiple Plans!
Grab It 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.