Loading...
Simulate deterministic percentage rollouts, sticky user bucketing, and A/B test sample size & duration
Load canary, staged rollout, and A/B test scenarios
Set flag identifier and target percentage
Users are deterministically assigned to buckets 0-99 via consistent hashing
// Next.js Middleware Edge Feature Flag
import { NextResponse, type NextRequest } from 'next/server';
function hashBucket(key: string): number {
let hash = 0x811c9dc5;
for (let i = 0; i < key.length; i++) {
hash ^= key.charCodeAt(i);
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
return Math.abs(hash >>> 0) % 100;
}
export function middleware(request: NextRequest) {
const userId = request.cookies.get('userId')?.value || request.ip || 'anonymous';
const bucket = hashBucket('new-checkout-v2:' + userId);
const isEnabled = bucket < 5; // 5% rollout
const response = NextResponse.next();
response.headers.set('X-Flag-new-checkout-v2', isEnabled ? 'true' : 'false');
return response;
}// Pure TypeScript Deterministic Flag Evaluator
export function isFeatureActive(userId: string): boolean {
let hash = 0x811c9dc5;
const key = 'new-checkout-v2:' + userId;
for (let i = 0; i < key.length; i++) {
hash ^= key.charCodeAt(i);
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
const bucket = Math.abs(hash >>> 0) % 100;
return bucket < 5;
}