Prompt
I need to run code at the edge before origin for auth and redirects
Latest observation
Oct 26, 2026
OpenAI APIWeb search: off
Yes — if you want to run logic before the request reaches your origin, you typically want an edge middleware / edge function / CDN worker.
What it can do
Common pre-origin tasks include:
- Auth checks: validate JWT/cookies/headers
- Redirects: locale, canonical URL, maintenance mode, A/B testing
- Rewrites: send requests to different backends without exposing it
- Header manipulation: add/remove headers before origin
- Bot / geo / device routing
Common options
Depending on your platform, this is usually called:
- Cloudflare Workers / Middleware
- AWS Lambda@Edge
- Fastly Compute@Edge
- Vercel Edge Middleware / Edge Functions
- Netlify Edge Functions
- Akamai EdgeWorkers
- NGINX/Envoy at the edge if you control the proxy layer
Typical flow
- Request hits CDN/edge
- Edge code runs
- It either:
- returns a response immediately (redirect/401/403/etc.), or
- rewrites/forwards to origin
- Origin only sees requests that passed your logic
Example pattern
Pseudo-flow:
if (!isAuthenticated(request)) {
return redirect("/login");
}
if (needsCanonicalRedirect(request)) {
return redirect("https://example.com" + canonicalPath);
}
return fetch(request); // continue to origin
Important notes
- Keep edge logic fast and stateless
- Avoid heavy CPU work or large dependencies
- Be careful with auth:
- validate signatures locally if possible
- avoid calling origin for every auth decision unless necessary
- Ensure cache behavior is correct so auth does not leak cached private content
If you tell me your platform, I can show the exact implementation for it.