Last update:2024-09-03 15:10:01
This Quick Start guides you through creating a simple Edge Cloud Apps Function to perform content rewriting based on the user agent. When a user visits your website using the Chrome browser, the page content will be rewritten; otherwise, the original content will be displayed.
Before you begin, ensure you have completed the following:
The test domain associated here is used solely for online debugging, effective in the testing environment, and won’t impact your production environment.
Using a Template can help you quickly create functions for common functionalities such as stream M3u8 rewriting, web link rewriting, and aggregated request responses. Each template has a description of its functionality. After selecting a template to create a function, you’ll see the corresponding function sample code directly in the CloudIDE interface. We are continuously updating the template library, and some other templates are still under development and will be available soon.
Within CloudIDE, you can freely write, edit, and debug your code. CloudIDE provides features such as syntax highlighting, auto-completion, and error prompts to help you write code efficiently. For more information on CloudIDE, please refer to our CloudIDE Documentation.
Below is a pre-written function that demonstrates how to return different page content based on the User-Agent:
export async function handleRequest(request) {
let response = await fetch(request)
response.headers.set("X-Version", "petshop-v1")
if (isFromChrome(request) && isHtml(response)) {
console.log("rewrite page")
let body = await response.text()
const en = true
body = body.replace('Make Your Pets Happy',
en ? ':::R:::E:::W:::R:::I:::T:::T:::E:::N:::')
body = body.replace('Dolore tempor clita lorem rebum kasd eirmod dolore diam eos kasd.',
en ? 'Only when accessed using Chrome browser will the text be rewritten.</br>For other browsers, the original text will be displayed.</br>' : 'The content will be rewritten only when accessed using the Chrome browser</br>Other browsers will display the original text.</br>')
response.headers.delete('ETag')
response.headers.delete('Last-Modified')
return new Response(body, response);
} else {
console.log("pass page")
return response
}
}
function isFromChrome(request) {
let userAgent = request.headers.get("User-Agent")
return userAgent && userAgent.toLowerCase().includes("chrome")
}
function isHtml(response) {
let contentType = response.headers.get("Content-Type")
return contentType && contentType.toLowerCase().includes("text/html")
}
addEventListener("fetch", event => {
return event.respondWith(handleRequest(event.request))
})
Each function can contain multiple JavaScript files to help you better organize and manage complex function logic. However, please note that each function must include an
index.js
file as the function entry point. When a function is triggered, the platform will begin executing code from theindex.js
file.
/ECA-test/pet-shop-website-template/index.html
. To configure request parameters, request path, headers, or body content, configure them one by one in the corresponding tabs of the test page to simulate real request scenarios.User Agent
as Chrome
and Firefox
, respectively:
Once you have finished debugging and met the testing requirements, you can deploy the function to the production environment. You can use Edge Cloud Apps’ Grayscale Deployment feature to preset batch deployment schedules, which are triggered by the platform at set intervals. Edge Cloud Apps allows you to configure custom batch sizes and proportions for gradual deployment, as well as batch activation times. If you want the changes to take effect immediately, simply set the activation time to the current time. For more details, please refer to the Grayscale Deployment documentation.
After the setup is complete, you can view the deployment records and current deployment status of your created function on the Grayscale Deployment interface. If you no longer need to deploy, you can click Stop to terminate your deployment schedule.
After configuring the trigger, your function officially takes effect on your acceleration domain configured on the CDNetworks platform. Next, you can monitor the function’s running status, such as the actual number of requests, average runtime, and error rate, through the Function Analysis page on the CDNetworks console.
When the User Agent
in the user’s request header is Chrome
:
When the User Agent
in the user’s request header is Firefox
: