Wait for server initialization
To manually trigger and wait for server initialization, use 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()
return hypertuneSource.root({
args: {
context: {
environment:
process.env.NODE_ENV === 'development'
? 'development'
: 'production',
user: {
id: 'e23cc9a8-0287-40aa-8500-6802df91e56a',
name: 'Example User',
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
To check for server initialization, use 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