Notice how we passed the ClientExample component a prop with the result of hypertune.dehydrate() on the server. This shows how you can immediately "bootstrap" the SDK on the client with the state of the SDK from the server. This lets you use your flag in the first app render without any page load delay, UI flicker or layout shift. This is optional; if you don't bootstrap the SDK on the client in this way, it will initialize as usual in the background and the useHypertune hook will trigger a re-render when it's done.
If you're using Hypertune in the browser and have a Content Security Policy, add the following URLs to the connect-src directive: https://edge.hypertune.com https://gcp.fasthorse.workers.dev
This allows analytics to be sent back to Hypertune so you can see how often different parts of your flag logic are called, 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.
7. (Optional) Include a build-time logic snapshot
If you don't "bootstrap" the SDK on the client with the state of the SDK from the server, as described above, and you try accessing your flag on the first app render, you'll get your hardcoded fallback value if the SDK hasn't had a chance to initialize from Hypertune Edge yet. This can result in a UI flicker or layout shift if the flag value changes when the SDK initializes. To avoid this, you can include a snapshot of your flag logic in the generated client as a build-time snapshot.
Add the following environment variable to your .env file:
HYPERTUNE_INCLUDE_INIT_DATA=true
Then regenerate the client.
The SDK will now instantly initialize from the snapshot first before fetching the latest flag logic from Hypertune Edge. And it will always successfully initialize, even if Hypertune Edge is unreachable. You can keep the snapshot fresh by setting up a webhook to regenerate the client on every Hypertune commit.
If you don't want to include the snapshot but still want to avoid UI flickers and layout shift, you can explicitly wait for initialization from Hypertune Edge with await hypertune.initIfNeeded() or check for initialization from Hypertune Edge with !!hypertune.getLastDataProviderInitTime().
8. (Optional) Use Vercel Edge Config
If your Gatsby 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:
GATSBY_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
Add the environment variables to your .env.development and .env.production files too.
Install the Vercel Edge Config package:
npminstall@vercel/edge-config
yarnadd@vercel/edge-config
pnpmadd@vercel/edge-config
Update your hypertune.ts to create an Edge Config client and pass it along with your Edge Config Item Key when initializing the Hypertune SDK: