Possible to A/B test between two targets under one CNAME?

Is it possible to dynamically route between two targets under one CNAME? I couldn’t find any specific documentation where Cloudflare workers or page rules could accomplish this.

For context my team is attempting an A/B test between two deployed Heroku apps as shown by “a-test.herokuapp.com”, which is the control and “b-test.herokuapp.com”, which is our B test.

I was hoping Cloudflare workers would be able to dynamically route between these two targets underneath the covers while keeping our URL the same e.g. app.domain.com.

Does anyone know if this is possible, and if so, is there any documentation around this? Thank you!

This is probably the best way to do it so you make sure a user is consistently directed to the same site.


Thanks for the documentation! I saw this recipe, but I was unsure whether the worker here would bypass the CNAME record I have setup? Would I have to remove the CNAME and allow the worker to process the request?

I don’t have a lot of experience with workers so I suppose I was confused by the example when the TEST_RESPONSE and CONTROL_RESPONSE both hit the same domain but just have different subpaths

const TEST_RESPONSE = new Response("Test group") *// e.g. await fetch("/test/somepath", request)*

const CONTROL_RESPONSE = new Response("Control group") *// e.g. await fetch("/control/somepath", request)*

I believe the Worker will trigger because that record is set to :orange:, though you could try an “A” record instead that points to as a place holder.