Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SECURITY.md #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Playgirlkaybraz11
Copy link

const NAME = "myExampleABTest";

export default {
async fetch(request) {
// Clone the original URL
const url = new URL(request.url);

// Enable Passthrough to allow direct access to control and test routes.
if (url.pathname.startsWith("/control") || url.pathname.startsWith("/test"))
  return fetch(request);

// Determine which group this requester is in.
const cookie = request.headers.get("cookie");

if (cookie && cookie.includes(`${NAME}=control`)) {
  url.pathname = "/control" + url.pathname;
} else if (cookie && cookie.includes(`${NAME}=test`)) {
  url.pathname = "/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 = "/control" + url.pathname;
  } else {
    url.pathname = "/test" + url.pathname;
  }
  // Reconstruct response to avoid immutability
  let response = await fetch(url);
  response = new Response(response.body, response);
  // Set cookie to enable persistent A/B sessions.
  response.headers.append("Set-Cookie", `${NAME}=${group}; path=/`);
  return response;
}
return fetch(url);

},
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant