Edge Functions provides a Serverless code execution environment on EdgeOne Edge nodes. You just need to write business function code and set trigger rules, without the need to configure and manage server infrastructure, to run code securely and with elastic scaling on Edge nodes close to users.
Strengths
Distributed deployment
EdgeOne has over 3,200 edge nodes, and edge functions run on edge nodes via distributed deployment.
Ultra-low latency
Client requests are automatically scheduled to the edge node closest to your users. If a cache hit triggers the edge function, the request is processed and the response result is sent to the client, significantly reducing access latency.
Elastic Scale-out
Edge functions can route client requests from closest to farthest based on sudden increases in request volume, processing them on edge nodes with sufficient computational resources. You don't need to worry about spikes occur.
Serverless Architecture
You no longer need to care about or maintain the memory, CPU, network, and other infrastructure resources of underlying servers, freeing up effort to focus on developing business code.
Quick Start
Create hello.ts under the ./edge-functions directory and use the following example code to create your first Edge Functions:
exportdefaultfunctiononRequest(context){
returnnewResponse('Hello from Edge Functions!');
}
Function Debugging
1. Install EdgeOne CLI: npm install -g edgeone
2. Local development: Under the Pages code project, execute edgeone pages dev to start the local service and perform function debugging.
3. Function release: Push code to the remote repository for auto-build function release.
For more ways to use EdgeOne CLI, see Documentation.
Routing
Edge Functions generates access routes based on the /edge-functions directory structure. You may create subdirectories at any level under the /edge-functions directory in the project repository. See the example below.
...
edge-functions
├── index.js
├── hello-pages.js
├── helloworld.js
├── api
├── users
├── list.js
├── geo.js
├── [id].js
├── visit
├── index.js
├── [[default]].js
...
The above directory file structure will generate the following routes after EdgeOne Pages platform construction. These routes map Pages URLs to /edge-functions files. When a client accesses the URL, it will trigger the corresponding file code to run:
File path
Routing
/edge-functions/index.js
example.com/
/edge-functions/hello-pages.js
example.com/hello-pages
/edge-functions/helloworld.js
example.com/helloworld
/edge-functions/api/users/list.js
example.com/api/users/list
/edge-functions/api/users/geo.js
example.com/api/users/geo
/edge-functions/api/users/[id].js
example.com/api/users/1024
/edge-functions/api/visit/index.js
example.com/api/visit
/edge-functions/api/[[default]].js
example.com/api/books/list
example.com/api/books/1024
example.com/api/...
Note:
The trailing slash / is optional. /hello-pages and /hello-pages/ will be routed to /edge-functions/hello-pages.js.
If no Edge Functions route is matched, client requests will be routed to the static resource of Pages.
- Routes are case-sensitive. /helloworld will be routed to /edge-functions/helloworld.js and cannot be routed to /edge-functions/HelloWorld.js.
Dynamic routing
Edge Functions support dynamic routing. In the above example, the first-level dynamic path is /edge-functions/api/users/[id].js, and the multi-level dynamic path is /edge-functions/api/[[default]].js. See the following usage:
File path
Routing
Match
/edge-functions/api/users/[id].js
example.com/api/users/1024
Yes
example.com/api/users/vip/1024
No
example.com/api/vip/1024
No
/edge-functions/api/[[default]].js
example.com/api/books/list
Yes
example.com/api/1024
Yes
example.com/v2/vip/1024
No
Function Handlers
Functions Handlers can create custom request handlers for Pages and define RESTful APIs to implement full-stack applications. Supported Handler methods:
params: dynamic routing /edge-functions/api/users/[id].js parameter value
exportfunctiononRequestGet(context){
returnnewResponse(`User id is ${context.params.id}`);
}
env: Pages environment variables
waitUntil: (task: Promise<any>): void; Used to notify the edge function to wait for the Promise to complete, extending the event handling lifecycle.
Runtime APIs
Edge Functions are based on Edge Functions, providing a Serverless code execution environment on EdgeOne Edge nodes. They support ES6 syntax and standard Web Service Worker API. Most Runtime APIs can be found in Edge function usage. See the description below:
Cache is designed based on the Web APIs standard Cache API. The Functions runtime environment will inject a global Cache object, which provides a group of Cache operation APIs.
Web Crypto API is designed based on the Web APIs standard Web Crypto API. It provides a group of common encryption APIs. Compared with pure JavaScript encryption APIs, Web Crypto API delivers higher performance.
Edge Function is a Serverless code execution environment designed and implemented based on the V8 JavaScript engine, providing the following standardized Web APIs.
Note:
Currently, fetch cannot be used to access EdgeOne node cache or origin in the EdgeOne CLI debugging environment.