Get flag updates

By default, SDKs check for updates to your flag logic in the background. When you save and activate a new commit from the Hypertune UI, SDKs will fetch your new flag logic and use it on the next flag evaluation.

To change the frequency at which the SDK checks for updates, set initDataRefreshIntervalMs in your createSource options:

src/components/AppHypertuneProvider.tsx
import { HypertuneProvider } from '../generated/hypertune.react'

export default function AppHypertuneProvider({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <HypertuneProvider
      createSourceOptions={{
        token: import.meta.env.VITE_HYPERTUNE_TOKEN!,
        initDataRefreshIntervalMs: 5_000,
      }}
      rootArgs={{
        context: {
          environment:
            process.env.NODE_ENV === 'development'
              ? 'development'
              : 'production',
          user: {
            id: 'e23cc9a8-0287-40aa-8500-6802df91e56a',
            name: 'Example User',
            email: '[email protected]',
          },
        },
      }}
    >
      {children}
    </HypertuneProvider>
  )
}

To disable checking for updates, set shouldRefreshInitData in your createSource options to false:

src/components/AppHypertuneProvider.tsx
import { HypertuneProvider } from '../generated/hypertune.react'

export default function AppHypertuneProvider({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <HypertuneProvider
      createSourceOptions={{
        token: import.meta.env.VITE_HYPERTUNE_TOKEN!,
        shouldRefreshInitData: false,
      }}
      rootArgs={{
        context: {
          environment:
            process.env.NODE_ENV === 'development'
              ? 'development'
              : 'production',
          user: {
            id: 'e23cc9a8-0287-40aa-8500-6802df91e56a',
            name: 'Example User',
            email: '[email protected]',
          },
        },
      }}
    >
      {children}
    </HypertuneProvider>
  )
}

Manually check for flag updates

To manually check for flag updates, 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.

This means 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.

Subscribe to flag updates

To add and remove listeners to be notified of updates, use the following methods:

hypertune.addUpdateListener(listener: (newStateHash: string) => void)
hypertune.removeUpdateListener(listener: (newStateHash: string) => void)

To get the current state hash, use the following method:

hypertune.getStateHash(): string | null

Last updated