Prompt
I need a way to run auth, redirects, and A/B tests at the edge
Latest observation
Oct 26, 2026
OpenAI APIWeb search: off
If you want auth, redirects, and A/B testing at the edge, the best approach is usually to put a CDN or edge runtime in front of your app and execute that logic in edge middleware / edge functions before the request reaches your origin.
Good options
1. Cloudflare Workers
Best if you want a very flexible, fast edge runtime.
Use for:
- authentication checks
- geo/device-based redirects
- A/B bucketing
- request/response rewriting
Pros:
- very low latency
- runs globally
- strong control over request handling
Cons:
- different runtime than Node.js
- may require adapting existing code
2. Vercel Edge Middleware / Functions
Best if you’re already on Vercel, especially with Next.js.
Use for:
- auth gating
- redirects
- A/B test assignment
- cookie-based routing
Pros:
- easy integration with Next.js
- simple deployment
- built for edge use cases
Cons:
- less flexible than a full edge runtime
- some Node APIs aren’t available
3. Netlify Edge Functions
Good for sites hosted on Netlify.
Use for:
- redirect logic
- auth enforcement
- experiment routing
4. Fastly Compute@Edge / Akamai / AWS Lambda@Edge
Good enterprise options if you need more infrastructure control.
Typical architecture
- User hits the edge
- Edge checks:
- is the user authenticated?
- should they be redirected?
- which A/B variant should they see?
- Edge sets cookies / headers if needed
- Edge forwards to origin or serves content directly
Common implementation pattern
- Auth: verify session cookie, JWT, or signed token
- Redirects: use path, country, device, or campaign rules
- A/B tests:
- assign variant in edge
- store variant in cookie
- keep assignment sticky across requests
Things to watch out for
- Don’t do heavy DB calls at the edge unless necessary
- Prefer JWTs, signed cookies, or cached session checks
- Make sure redirects don’t create loops
- Keep A/B assignment deterministic and sticky
- Consider caching behavior carefully so variants don’t leak
If you want the simplest practical setup
- Next.js app on Vercel
- use Edge Middleware for auth/redirects/experiments
- store experiment assignment in a cookie
- use a server-side auth provider like Clerk, Auth.js, or custom JWTs
If you want, I can also give you:
- a reference architecture,
- a Next.js Edge Middleware example, or
- a Cloudflare Workers example.