javascript - 如何与 Cloudflare 工作人员进行 A/B 测试

标签 javascript cloudflare cloudflare-workers

我正在寻找一个示例,说明这两行代码在正常运行的 A/B 测试工作人员中是什么样子。来自 https://developers.cloudflare.com/workers/examples/ab-testing

const TEST_RESPONSE = new Response("Test group") // e.g. await fetch("/test/sompath", request)
const CONTROL_RESPONSE = new Response("Control group") // e.g. await fetch("/control/sompath", request)

我使用了示例,替换了我正在使用的路径,并收到语法错误,指出等待只能在异步中使用。所以我将函数更改为异步函数handleRequest(request)并得到了500错误。

这两行代码应该是什么样子才能工作?

最佳答案

工作示例

document.getElementById("myBtn").addEventListener("click", myFun);



async function myFun() {
  const isTest = Math.random() > 0.5 //here you place your condition
  const res = isTest ? await fetch1() : await fetch1() //here you should have different fetches for A and B
  console.log(res)
  document.getElementById("demo").innerHTML = res.message;
}

async function fetch1() {
  //I took the link from some other SO answer as I was running into CORS problems with e.g. google.com
  return fetch("https://currency-converter5.p.rapidapi.com/currency/list?format=json", {
      "method": "GET",
      "headers": {
        "x-rapidapi-host": "currency-converter5.p.rapidapi.com",
        "x-rapidapi-key": "**redacted**"
      }
    })
    .then(response => response.json())
}
<button id="myBtn">button</button>
<div id="demo">demo</div>
<script src="https://unpkg.com/@babel/standalone@7/babel.min.js"></script>
<script type="text/babel">

</script>

关于javascript - 如何与 Cloudflare 工作人员进行 A/B 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68574677/

相关文章:

javascript - 为什么这段代码不显示背景图片?

javascript - 无法从 Firebase 更新和删除对象

amazon-web-services - 用于限制对 Cloudflare IP 地址的访问的 S3 存储桶策略

angular - 如何让 Angular 应用程序与 Cloudflare Workers - Workers Sites 一起工作

github-pages - 如何使用 Cloudflare Workers 优化静态网站的 TTFB?

javascript - 我在页面加载时将整个 $_SESSION 变量放入 json 对象中。虽然这对我有用,但这是一个好的做法吗?

javascript - 在 python 中, "export"来自模块的定制对象

github-pages - 错误 404 这里没有 GitHub Pages 站点

php - Cloudflare 在我的服务器上抛出 524 错误