To access flags in the browser in Client Components, first wrap your app with the generated <HypertuneProvider> component in your root layout in app/layout.tsx:
Note how we pass serverDehydratedState to <HypertuneProvider>. This instantly hydrates or "bootstraps" the SDK in the browser with the state of the SDK on the server so you can use your flags in the first app render with no layout shift, UI flickers or page load delay. This is optional — if you don't pass this prop, the SDK will initialize as usual in the background and <HypertuneProvider> will trigger a re-render when it's done.
Also note how we pass serverRootArgs to <HypertuneProvider>. This lets you reuse the root args that you used in getHypertune on the server. This is optional — you can also manually create root args for <HypertuneProvider>.
If you have a Content Security Policy, add the following URLs to the connect-src directive: https://edge.hypertune.com https://gcp.fasthorse.workers.dev
This lets the browser send analytics back to Hypertune so you can see how often different parts of your flag logic are evaluated, e.g. to see how many sessions fall into each targeting rule, as well as analytics for your events, A/B tests and machine learning loops.
To access flags in Route Handlers, use the getHypertune function:
To improve reliability, you can include a snapshot of your flag logic in the generated client at build time. The SDK will instantly initialize from the snapshot first before fetching the latest flag logic from Hypertune Edge.
Add the following environment variable to your .env file:
HYPERTUNE_INCLUDE_INIT_DATA=true
Then regenerate the client.
You can keep the snapshot fresh by setting up a webhook to regenerate the client on every Hypertune commit. In this case, you don't need to initialize from Hypertune Edge at all, eliminating network latency and bandwidth, improving both performance and efficiency.
6. (Optional) Use Vercel Edge Config
If your Next.js app is deployed on Vercel, you can use Edge Config to initialize the Hypertune SDK on the server with near-zero latency.
Connect your Hypertune project to a new or existing Edge Config store. Copy the displayed environment variables for later. They contain your Hypertune Token, Edge Config Connection String and Edge Config Item Key.
Go to your Vercel dashboard and select the project you want to use the Hypertune integration with. Go to Settings > Environment Variables and add the following:
NEXT_PUBLIC_HYPERTUNE_TOKEN, set to your Hypertune Token
EDGE_CONFIG, set to your Edge Config Connection String
EDGE_CONFIG_HYPERTUNE_ITEM_KEY, set to your Edge Config Item Key
2. Use the integration
Pull the environment variables to your .env.development.local file by running:
vercel env pull .env.development.local
Install the @vercel/edge-config package:
npminstall@vercel/edge-config
yarnadd@vercel/edge-config
pnpmadd@vercel/edge-config
Finally, update your getHypertune function to create an Edge Config client and pass it along with your Edge Config Item Key when creating the Hypertune source:
Replace @/lib/getHypertune with the relative path to your getHypertune function from your generated output directory.
Then regenerate the client:
npxhypertune
yarnhypertune
pnpmhypertune
Update your getHypertune function to accept a params object argument with headers and cookies properties, and respect flag overrides set by the Vercel Toolbar:
import"server-only";import { ReadonlyHeaders } from"next/dist/server/web/spec-extension/adapters/headers";import { ReadonlyRequestCookies } from"next/dist/server/web/spec-extension/adapters/request-cookies";import { unstable_noStore as noStore } from"next/cache";import { createSource } from"@/generated/hypertune";import { getVercelOverride } from"@/generated/hypertune.vercel";consthypertuneSource=createSource({ token:process.env.NEXT_PUBLIC_HYPERTUNE_TOKEN!,});exportdefaultasyncfunctiongetHypertune(params?: { headers?:ReadonlyHeaders; cookies?:ReadonlyRequestCookies;}) {noStore();awaithypertuneSource.initIfNeeded(); // Check for flag updates// Respect flag overrides set by the Vercel ToolbarhypertuneSource.setOverride(awaitgetVercelOverride());returnhypertuneSource.root({ args: { context: { environment:process.env.NODE_ENV, user: { id:"1", name:"Test", email:"hi@test.com" }, }, }, });}