Skip to the code

A/B test the home page with a cookie

Mediumproxy.ts

Leaf and Kettle has a new home page and wants to know whether it sells more tea than the old one. Both are built: app/home-a/page.tsx is the current page and app/home-b/page.tsx the new one. Write proxy.ts so each visitor of / sees one of them, half and half, while the address bar keeps saying /.

config.matcher makes the proxy run for the home page, /, and nothing else.

A visitor whose home-test cookie is a or b is already in that group: rewrite to /home-a or /home-b, keeping the query string, so /?utm_source=mail shows /home-b?utm_source=mail. Their cookie stays as it is, so don't set it again.

Anyone else, with no cookie or a value that isn't a or b, joins a group at random: a when Math.random() is below 0.5, b otherwise. Rewrite to that group's page, and save the group in the home-test cookie on the response, with path: "/", a maxAge of 30 days in seconds, httpOnly, and sameSite: "lax", so they see the same home page for a month.

The tests read a rewrite's target from the response's x-middleware-rewrite header, where NextResponse.rewrite puts it and where Next.js's own getRewrittenUrl test helper reads it, and the cookie with response.cookies.get("home-test").

GET /?utm_source=mail                    (no cookie, and Math.random() gives 0.73)
→ shows /home-b?utm_source=mail, while the address stays /?utm_source=mail
  and sets the cookie home-test=b for 30 days
 
GET /   Cookie: home-test=a
→ shows /home-a, and sets no cookie

What the tests check

  1. runs for the home page only
    expect(proxyRuns("/")).toBe(true);
    expect(proxyRuns("/?utm_source=mail")).toBe(true);
    for (const path of ["/teas", "/home-a", "/home-b", "/api/teas"]) expect(proxyRuns(path)).toBe(false);
    
  2. a new visitor joins a group at random and sees its page at /
    const random = Math.random;
    try {
      Math.random = () => 0.2;
      const first = await proxy(request("/"));
      expect(first.headers.get("x-middleware-rewrite")).toBe("http://localhost:3000/home-a");
      expect(first.status).toBe(200);
      expect(first.headers.get("location")).toBeNull();
      Math.random = () => 0.73;
      const second = await proxy(request("/"));
      expect(second.headers.get("x-middleware-rewrite")).toBe("http://localhost:3000/home-b");
      Math.random = () => 0.5;
      const third = await proxy(request("/"));
      expect(third.headers.get("x-middleware-rewrite")).toBe("http://localhost:3000/home-b");
    } finally {
      Math.random = random;
    }
    
  3. saves a new visitor's group for 30 days in an httpOnly cookie
    const random = Math.random;
    try {
      Math.random = () => 0.2;
      const response = await proxy(request("/"));
      const cookie = response.cookies.get("home-test");
      expect(cookie?.value).toBe("a");
      expect(cookie.path).toBe("/");
      expect(cookie.maxAge).toBe(60 * 60 * 24 * 30);
      expect(cookie.httpOnly).toBe(true);
      expect(String(cookie.sameSite).toLowerCase()).toBe("lax");
      expect(response.headers.getSetCookie()).toHaveLength(1);
      Math.random = () => 0.9;
      expect((await proxy(request("/"))).cookies.get("home-test")?.value).toBe("b");
    } finally {
      Math.random = random;
    }
    
  4. a returning visitor keeps their group, with no new cookie and no new draw
    const random = Math.random;
    let draws = 0;
    try {
      Math.random = () => {
        draws++;
        return 0.2;
      };
      for (const group of ["a", "b"]) {
        const response = await proxy(request("/", { headers: { cookie: "home-test=" + group } }));
        expect(response.headers.get("x-middleware-rewrite")).toBe("http://localhost:3000/home-" + group);
        expect(response.headers.getSetCookie()).toEqual([]);
      }
      expect(draws).toBe(0);
    } finally {
      Math.random = random;
    }
    
  5. a cookie with any other value is replaced
    const random = Math.random;
    try {
      Math.random = () => 0.1;
      for (const value of ["c", "B", ""]) {
        const response = await proxy(request("/", { headers: { cookie: "home-test=" + value } }));
        expect(response.headers.get("x-middleware-rewrite")).toBe("http://localhost:3000/home-a");
        expect(response.cookies.get("home-test")?.value).toBe("a");
      }
    } finally {
      Math.random = random;
    }
    
  6. keeps the query string
    const response = await proxy(request("/?utm_source=mail&utm_campaign=spring", { headers: { cookie: "home-test=b" } }));
    expect(response.headers.get("x-middleware-rewrite")).toBe("http://localhost:3000/home-b?utm_source=mail&utm_campaign=spring");
    

Learn more

proxy.tsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).