const NAME = "myExampleABTest";
async function handleRequest(req) {
const url = new URL(req.url);
// Enable Passthrough to allow direct access to control and test routes.
if (url.pathname.startsWith("/control") || url.pathname.startsWith("/test"))
return fetch(req, {cdnProxy: false});
// Determine which group this requester is in.
const cookie = req.headers.get("cookie");
if (cookie && cookie.includes(`${NAME}=control`)) {
url.pathname = "/ab-control" + url.pathname;
} else if (cookie && cookie.includes(`${NAME}=test`)) {
url.pathname = "/ab-test" + url.pathname;
} else {
// If there is no cookie, this is a new client. Choose a group and set the cookie.
const group = Math.random() < 0.5 ? "test" : "control"; // 50/50 split
if (group === "control") {
url.pathname = "/ab-control" + url.pathname;
} else {
url.pathname = "/ab-test" + url.pathname;
}
// Reconstruct response to avoid immutability
let res = await fetch(url, {cdnProxy: false});
res = new Response(res.body, res);
// Set cookie to enable persistent A/B sessions.
res.headers.append("Set-Cookie", `${NAME}=${group}; path=/`);
return res;
}
return fetch(url, {cdnProxy: false});
}
addEventListener("fetch", event => {
return event.respondWith(handleRequest(event.request))
})