Pages edge functions provide 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.
Advantages of Edge Functions
Distributed deployment
EdgeOne has over 3,200 edge nodes, and edge functions run on edge nodes via distributed deployment.
Ultra-low latency
Client requests will be automatically scheduled to the edge node closest to your users. When a trigger rule is hit, the edge function processes the request and returns the response result to the client, significantly reducing access latency.
Elastic Scale-out
Edge functions can route client requests from closest to farthest based on a sudden increase in request volume, processing them on edge nodes with sufficient computational resources. You have no need to worry when spikes occur.
Serverless Architecture
You no longer need to care about or maintain the memory, CPU, network, and other infrastructure resources of the underlying server, freeing up effort to focus on business code development.
Quick Start Guide
1. Install the npm package: npm install -g edgeone. For more commands, see the scaffolding document.
2. Local development: Under the Pages code project
2.1 Function initialization: edgeone pages init, automatically initializes the functions directory to handle functions code.
2.2 Associate project: edgeone pages link, fill in the current project name, automatically associate project KV configuration, environment variables, etc.
2.3 Local development: edgeone pages dev, start up the local proxy service to perform function debugging
3. Function release: Push code to the remote repository for auto-building and function release.
Routing
Pages Functions generates access routes based on the /functions directory structure. You can create subdirectories at any level under the /functions directory in your project repository. See the example below.
...
functions
├── index.js
├── hello-pages.js
├── helloworld.js
├── api
├── users
├── list.js
├── geo.js
├── [id].js
├── visit
├── index.js
├── [[default]].js
...
The file structure in the above directory will generate the following routes after platform construction by EdgeOne Pages. These routes map Pages URLs to the /functions file. When a client accesses the URL, it will trigger the corresponding file code to run:
File path
Routing
/functions/index.js
example.com/
/functions/hello-pages.js
example.com/hello-pages
/functions/helloworld.js
example.com/helloworld
/functions/api/users/list.js
example.com/api/users/list
/functions/api/users/geo.js
example.com/api/users/geo
/functions/api/users/[id].js
example.com/api/users/1024
/functions/api/visit/index.js
example.com/api/visit
/functions/api/[[default]].js
example.com/api/books/list
example.com/api/books/1024
example.com/api/...
Note:
- The trailing slash / in routing is optional. /hello-pages and /hello-pages/ will be routed to /functions/hello-pages.js.
If no Pages Functions route is matched, client requests will be routed to the corresponding static resource of Pages.
- Routing is case-sensitive. /helloworld will be routed to /functions/helloworld.js and cannot be routed to /functions/HelloWorld.js.
Dynamic Routing
Pages Functions support dynamic routing. In the above example, the single-level dynamic path is `/functions/api/users/[id].js`, and the multi-level dynamic path is `/functions/api/[[default]].js`. See the following usage:
File path
Routing
Match
/functions/api/users/[id].js
example.com/api/users/1024
Yes
example.com/api/users/vip/1024
No
example.com/api/vip/1024
No
/functions/api/[[default]].js
example.com/api/books/list
Yes
example.com/api/1024
Yes
example.com/v2/vip/1024
No
Function Handlers
Using Functions Handlers can create custom request handlers for Pages and define RESTful APIs to implement full-stack applications. Supported Handler methods:
The context is an object passed to Function Handlers methods, containing these properties:
request: client request object
params: dynamic routing /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 completion, extending the event handling lifecycle.
Runtime APIs
Pages Functions are implemented 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 interfaces.
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 implementation, Web Crypto API delivers higher performance.
Edge Function is a Serverless code execution environment designed and implemented based on the V8 JavaScript engine. It provides the following standardized Web APIs.
Note:
Currently, Edgeone CLI does not support using fetch to access Edgeone node cache or origin in the debugging environment.
Use context.request.eo to get the client's GEO info.
Pages Functions cannot be used with addEventListener. Listen to client requests based on Function Handlers.