Created
October 15, 2024 13:44
-
-
Save mfrachet/8a610f581d10ba81e32a4a47ad52974f to your computer and use it in GitHub Desktop.
Vercel feature flags with Flag Engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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