Wait for server initialization
You can manually trigger and wait for server initialization with the initIfNeeded
method:
import { createSource } from "../generated/hypertune";
const hypertuneSource = createSource({
token: process.env.HYPERTUNE_TOKEN!,
initDataRefreshIntervalMs: 5_000,
});
export default async function getHypertune() {
await hypertuneSource.initIfNeeded(); // Check for flag updates
return hypertuneSource.root({
args: {
context: {
environment:
process.env.NODE_ENV === "development"
? "development"
: "production",
user: { id: "1", name: "Test", email: "[email protected]" },
},
},
});
}
When using initIfNeeded
, the initDataRefreshIntervalMs
option specifies the minimum time between initialization requests.
For example, if you set this to 5_000
, initIfNeeded
will only trigger a new initialization request if the last one was over 5 seconds ago.
So you can await initIfNeeded
on every backend request to ensure flag values are fresh while minimizing network latency and bandwidth.
This is particularly useful in serverless and edge environments like Vercel deployments, Cloudflare Workers, AWS Lambdas, etc, where background SDK tasks like fetching updates aren't guaranteed to execute.
Check for server initialization
You can also check for server initialization with the getLastInitDataRefreshTime
method:
import { useHypertune } from "../generated/hypertune.react";
export default function ClientComponent() {
const hypertune = useHypertune();
if (!hypertune.getLastInitDataRefreshTime()) {
return null;
}
const exampleFlag = hypertune.exampleFlag({ fallback: false });
return <div>Example Flag: {String(exampleFlag)}</div>;
}
Last updated