边缘开发者平台
  • 边缘函数
    • 概述
    • 快速指引
    • 操作指引
      • 函数管理
      • Web调试
      • 触发配置
      • 环境变量
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • 示例函数
      • 示例概述
      • 301重定向
      • 获取客户端URL信息
      • 基于客户端地理位置的自定义
      • 获取客户端地理位置信息
      • 批量重定向
      • 返回 HTML 页面
      • 返回 JSON
      • Fetch 远程资源
      • 请求头鉴权
      • 修改响应头
      • AB 测试
      • 设置 Cookie
      • 基于请求区域重定向
      • Cache API 使用
      • 缓存 POST 请求
      • 流式响应
      • 合并资源流式响应
      • 防篡改校验
      • m3u8 改写与鉴权
      • 图片自适应缩放
      • 图片自适应 WebP
      • 自定义 Referer 限制规则
      • 远程鉴权
      • HMAC 数字签名
      • 自定义下载文件名
      • 获取客户端 IP
    • 最佳实践
      • 通过边缘函数实现自适应图片格式转换
当前内容仅提供英语版本,中文版我们将尽快补充,感谢您的理解。

基于客户端地理位置的自定义

This example captures incoming HTTP requests and provides customized welcome messages and geo location (latitude and longitude) information based on the country where the client is located. It can be used for offering global personalized customization experience to users.

Sample Code

// Add a fetch event listener, which is triggered when a request is incoming. It uses the handleRequest function to handle requests and return responses.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
function handleRequest(request) {
// Get the country code provided by EdgeOne.
const countryCode = request.eo.geo.countryCodeAlpha2;
// Select the corresponding language and welcome message based on the country code.
let responseText;
switch (countryCode) {
case 'CN': // China
responseText =`Hello user from China! Your latitude and longitude are ${request.eo.geo.latitude},${request.eo.geo.longitude}`;
break;
case 'KR': // South Korea
responseText = `한국의 사용자님 반갑습니다! 당신의 위도와 경도는 ${request.eo.geo.latitude}와 ${request.eo.geo.longitude}입니다.`;
break;
case 'DE': // Germany
responseText = `Willkommen in Deutschland! Ihre Breiten- und Längengrad sind ${request.eo.geo.latitude} und ${request.eo.geo.longitude}.`;
break;
case 'US': // USA
responseText = `Hello from the USA! Your latitude and longitude are ${request.eo.geo.latitude} and ${request.eo.geo.longitude}.`;
break;
default: // Respond in English by default in other cases.
responseText = `Welcome to our service! Your latitude and longitude are ${request.eo.geo.latitude} and ${request.eo.geo.longitude}.`;
break;
}
// Return a response.
return new Response(responseText, {
headers: {
'Content-Type': 'text/plain;charset=UTF-8'
}
})
}

Sample Preview

In the address bar of the browser, enter a URL that matches a triggering rule of the edge function to preview the effect of the sample code. If the client of the current request URL is located in China, the browser will respond with the welcome page for China, as well as the latitude and longitude of the client geo location.


Related References