nNext.js

Overview

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.

Setup the SDK

Install Packages

npm install @neonblue-ai/next @neonblue-ai/react-bindings

Initialize the SDK

Wrap your application with NeonBlueBootstrapProvider in your root layout. This is an async Server Component that fetches experiment data on the server:

// app/layout.tsx
import { NeonBlueBootstrapProvider } from "@neonblue-ai/next";

export default async function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <NeonBlueBootstrapProvider
          clientKey="client--00000000-0000-0000-0000-000000000000"
          user={{ userId: "00000000" }}
        >
          {children}
        </NeonBlueBootstrapProvider>
      </body>
    </html>
  );
}

Use the SDK

useExperiment Hook

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

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.