A/B testing
Split traffic between two responses in a function, picking a group at random and recording it in a cookie.
Serve one of two responses from a function on Azion’s global network, choosing the group at random and recording it in a cookie. Use this pattern to run randomized experiments, such as comparing two page variants, without sending traffic to an origin to decide which version to show.
How it works
The fetch handler reads the incoming Cookie header with request.headers.get("cookie"). When the header contains TestA/B=a the function returns the control response, and when it contains TestA/B=b it returns the test response. When neither is present, Math.random() picks the test or control group at roughly 50/50, and response.headers.append("Set-Cookie", ...) writes the chosen group into the TestA/B cookie so the browser sends it on the next request. event.respondWith() then returns that Response to the client.