Skip to main content

How the Rule Engine Works

Every Middlewr link can run a rule engine. Instead of a static redirect, the link evaluates a compiled decision tree against the incoming request — routing visitors to the right destination based on who they are, where they are, and what device they’re using.

Visual Authoring

Build rule graphs in a drag-and-drop flow editor. Chain conditions, splits, transforms, destinations, password gates, countdowns, and webhooks visually.

Edge Compilation

Rule graphs compile to flat decision trees (~250 bytes for a 4-node graph). Stored in Cloudflare KV, evaluated in microseconds.

Real-Time Evaluation

Every click triggers an evaluation at the nearest Cloudflare edge location. Sub-100ms redirects worldwide.

Per-Branch Analytics

Track which destination each visitor was routed to. Measure performance per rule branch in real-time.

Architecture

1

Author

Build a rule graph in the visual flow editor. Drag conditions, splits, transforms, and destinations onto the canvas and connect them.
2

Compile

On save, the API validates the graph (max 50 nodes, no cycles, valid weights) and compiles it to a flat decision tree with index-based pointers.
3

Deploy

The compiled tree is embedded in the link’s edge value and written to Cloudflare KV. Deployment is instant — no build step, no propagation delay.
4

Evaluate

On every click, the edge worker loads the compiled tree from KV and evaluates it against the request context (geo, device, headers, time). The matched destination is returned as a redirect.

Eight Node Types

Entry Node

The starting point of every rule graph. Controls the activation window with optional start and expiration dates, and defines a fallback URL for visitors who arrive outside the schedule.

Condition Nodes

Evaluate a visitor attribute and branch to true or false. Chain conditions to build complex targeting logic.

Split Nodes

Distribute traffic across two or more variants by weight. Uses deterministic FNV-1a hashing on visitorId + linkId so the same visitor always sees the same variant.

Transform Nodes

Modify the redirect without changing the destination. Accumulate transforms as the tree is traversed.
  • Query parameters — Inject or override URL parameters (e.g., add utm_source=email)
  • Redirect status — Override the HTTP status code (301, 302, 307, 308)
  • Response headers — Set or append custom HTTP headers on the redirect response

Destination Nodes

Terminal nodes that specify the final redirect URL. Every rule path must end at a destination or fall back to the link’s default URL.

Password Node

Gates the redirect behind a password challenge. When reached, evaluation stops and the visitor must enter the correct password before the rule engine continues.

Countdown Node

Pauses the redirect and shows a countdown timer with an optional message. Duration is configurable from 1 to 300 seconds.

Webhook Node

Fires an HTTP request (POST or GET) to an external URL. The call is queued asynchronously — the redirect is not delayed. Use it to notify your backend, trigger workflows, or log events.

Validation

Before compiling, Middlewr validates your rule graph and surfaces errors in the editor:

Safety at the Edge

The edge evaluator is designed to never break a redirect:
  • Max 100 evaluation steps — prevents infinite loops from malformed trees
  • Try/catch fallback — any evaluation error falls back to the link’s default destination
  • Prototype pollution guard — field resolution rejects __proto__, constructor, prototype
  • String coercion — handles type mismatches gracefully between Cloudflare properties

Next Steps

Conditional Routing

Explore all supported conditions: geo, device, browser, language, time, and more.

Edge Transforms

Inject query parameters, override redirects, and set response headers at the edge.

Password Protection

Gate access to destinations with password verification at the edge.

Webhooks

Fire HTTP requests to external systems as visitors traverse your rule graph.