Skip to content

Instantly share code, notes, and snippets.

@mfrachet
Created October 15, 2024 13:44
Show Gist options
  • Save mfrachet/8a610f581d10ba81e32a4a47ad52974f to your computer and use it in GitHub Desktop.
Save mfrachet/8a610f581d10ba81e32a4a47ad52974f to your computer and use it in GitHub Desktop.
Vercel feature flags with Flag Engine
import { createFlagEngine } from "@flag-engine/core";
import crypto from "node:crypto";
import { headers } from "next/headers";
import { unstable_flag as flag } from "@vercel/flags/next";
const engine = createFlagEngine([
{
key: "summer-sale",
status: "enabled",
strategies: [
{
name: "All audience",
rules: [],
variants: [
{
name: "A",
percent: 50,
},
{
name: "B",
percent: 50,
},
],
},
],
},
]);
const hash = (str: string) =>
crypto.createHash("sha256").update(str).digest("hex");
export const showSummerSale = flag({
key: "summer-sale",
decide: () => {
const headersMap = headers();
const ua = headersMap.get("user-agent") || "Unknown UA";
const userId = hash(ua);
const userCtx = engine.createUserContext({
__id: userId,
});
return userCtx.evaluate("summer-sale");
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment