The Next.js integration provides server-side rendering (SSR) support using a bootstrap pattern. Experiment data is fetched on the server during the initial render and passed to the client, avoiding hydration mismatches.
Wrap your application with NeonBlueBootstrapProvider in your root layout. This is an async Server Component that fetches experiment data on the server:
Use the useExperiment hook from @neonblue-ai/react-bindings in your Client Components:
Async Experiment with Context
For experiments that aren't in the initial cache (cache miss), the SDK fetches from the network. You can pass additional context for these requests:
The context is only sent on cache misses when fetching from the network.
useNeonBlueClient Hook
Access the client directly for more control:
Configuration
Provider Props
Prop
Type
Default
Description
clientKey
string
required
Your Neon Blue SDK key
user
NeonBlueUser
required
User object with identifier
useCookie
boolean
true
Enable cookie-based stableId persistence
clientOptions
NeonBlueOptions
null
Additional client configuration
Cookie-Based StableId
By default, useCookie is enabled. This ensures consistent experiment assignments across server and client renders by following these steps:
1
Read stableId from cookies on the server (if present)
The provider will attempt to read an existing stableId from cookies during server rendering.
2
Generate a new stableId if none exists
If no stableId is present, a new one will be generated to ensure assignment stability.
3
Persist the stableId to cookies on the client
The generated or existing stableId is saved to cookies on the client to maintain consistency across renders.
To disable this behavior:
Custom API Endpoint
Configure a custom API endpoint via clientOptions:
How It Works
The bootstrap pattern pre-populates the client cache with server-fetched data:
1
Server Render
NeonBlueBootstrapProvider fetches experiment data via initializeAsync() during server rendering.
2
Serialization
Data is serialized to JSON and passed to the client as part of the HTML payload.
3
Client Hydration
BootstrapClientSubProvider initializes the client with the bootstrapped data so it does not need to make an immediate network call.
4
Hooks Ready
useExperiment checks the pre-populated cache first and only fetches from the network on a cache miss.
Changing Users
The NeonBlueBootstrapProvider is a Server Component, so user changes require a page refresh to re-run on the server. For client-side login/logout without page refresh, use the NeonBlueProvider from @neonblue-ai/react-bindings instead:
Then use in components:
The provider automatically re-initializes with the new user and fetches fresh experiment assignments.