Skip to main content

@xaiku/nextjs

Next.js integration for Xaiku with both client and server support. This package provides two entry points: a client module that re-exports all @xaiku/react components and hooks, and a server module with a React Server Component provider, middleware, and a server-side SDK factory.

Installation

npm install @xaiku/nextjs

Client Entry Point

import makeSDK, { XaikuProvider, Text, Experiment, ... } from '@xaiku/nextjs';

The default import (@xaiku/nextjs) is a 'use client' module. It re-exports everything from @xaiku/react and provides a Next.js-specific factory function.

Factory Function

makeSDK(options?: SDKOptions): BrowserSDKInstance

Creates a browser SDK instance with framework set to 'nextjs' and frameworkVersion set to the installed Next.js version. Accepts the same options as @xaiku/browser.

import makeSDK from '@xaiku/nextjs';

const sdk = makeSDK({ pkey: 'pk_your_public_key' });

Re-exported Components and Hooks

All of the following are re-exported directly from @xaiku/react:

ExportDescription
XaikuProviderClient-side provider component.
XaikuContextReact context holding the SDK instance.
ExperimentExperiment scope provider.
useExperimentIdHook to read the current experiment ID from context.
TextVariant text component.
useTextHook to resolve variant text.
useTrackViewHook to track view impressions.
useTrackClickHook that returns a click tracking callback.
useTrackConversionHook that returns a conversion tracking callback.

See the @xaiku/react reference for full documentation of each export.

Server Entry Point

import makeSDK, { XaikuProvider, xaikuMiddleware } from '@xaiku/nextjs/server';

The server entry point (@xaiku/nextjs/server) provides utilities for React Server Components and Next.js middleware.

XaikuProvider (Server)

An async React Server Component that initializes the SDK on the server, fetches experiments, and passes them to the client-side provider. This eliminates the loading flash that occurs when experiments are fetched client-side.

// app/layout.js
import { XaikuProvider } from '@xaiku/nextjs/server';

export default function RootLayout({ children }) {
return (
<html>
<body>
<XaikuProvider>
{children}
</XaikuProvider>
</body>
</html>
);
}

Props:

PropTypeRequiredDefaultDescription
pkeystringNoprocess.env.NEXT_PUBLIC_XAIKU_API_KEYPublic key. Defaults to the environment variable.
userIdstringNoUser identifier, also used as GUID fallback.
sdkSDKInstanceNoPre-initialized server SDK instance.
childrenReactNodeYesChild components.
...restobjectNoAdditional options forwarded to makeSDK() and the client provider.

Behavior:

  1. Reads the GUID from cookies (using the __xaiku__guid__ cookie key).
  2. Creates a server SDK instance (or uses the provided sdk prop).
  3. Fetches experiments on the server via sdk.getExperiments().
  4. Renders the client-side XaikuProvider with experiments and guid pre-populated, avoiding a client-side fetch.

xaikuMiddleware

An async middleware function that ensures a GUID cookie is set for every request. Use it in your Next.js middleware to establish visitor identity before any page renders.

// middleware.js
import { xaikuMiddleware } from '@xaiku/nextjs/server';

export async function middleware(request) {
return await xaikuMiddleware(request);
}

export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
};

Signature:

xaikuMiddleware(request: NextRequest, options?: object): Promise<NextResponse>

Parameters:

ParameterTypeRequiredDescription
requestNextRequestYesThe incoming Next.js request object.
optionsobjectNoAdditional SDK options (e.g., pkey).

Behavior:

  1. Reads the existing GUID from cookies.
  2. Creates a server SDK instance to validate or generate the GUID.
  3. Sets the GUID cookie on the response (path: '/').
  4. Returns NextResponse.next() to continue the request.

The middleware uses process.env.NEXT_PUBLIC_XAIKU_API_KEY as the default public key. Override it by passing pkey in the options.

makeSDK (Server)

An async factory function for creating a server-side SDK instance. Built on @xaiku/node with Next.js-specific defaults.

import makeSDK from '@xaiku/nextjs/server';

const sdk = await makeSDK({ pkey: 'pk_your_public_key', guid: 'user-guid' });
const experiments = await sdk.getExperiments();

Signature:

makeSDK(options?: object): Promise<SDKInstance>

Default behavior:

OptionDefaultDescription
pkeyprocess.env.NEXT_PUBLIC_XAIKU_API_KEYReads from the environment variable if not provided.
framework'nextjs'Framework identifier.
frameworkVersionDetected from next/package.jsonNext.js version.
skipClienttrueSkips client creation (not needed server-side).
skipExperimentstrueSkips automatic experiment fetching during init (experiments are fetched explicitly).

All other options are forwarded to @xaiku/node.

Configuration

Environment Variables

VariableDescription
NEXT_PUBLIC_XAIKU_API_KEYYour Xaiku public key. Used as the default pkey by both the server provider and middleware.

For the best experience with Next.js App Router:

  1. Add NEXT_PUBLIC_XAIKU_API_KEY to your environment.
  2. Set up the middleware to manage GUID cookies.
  3. Use the server XaikuProvider in your root layout for server-side experiment fetching.
  4. Use Text, Experiment, and tracking hooks in client components.
// middleware.js
import { xaikuMiddleware } from '@xaiku/nextjs/server';

export async function middleware(request) {
return await xaikuMiddleware(request);
}

// app/layout.js
import { XaikuProvider } from '@xaiku/nextjs/server';

export default function RootLayout({ children }) {
return (
<html>
<body>
<XaikuProvider>{children}</XaikuProvider>
</body>
</html>
);
}

// app/page.js (client component)
'use client';
import { Experiment, Text, useTrackClick } from '@xaiku/nextjs';

export default function Page() {
const trackClick = useTrackClick({ experimentId: 'exp_1', partId: 'cta' });

return (
<Experiment id="exp_1">
<Text id="headline" fallback="Welcome" />
<button onClick={trackClick}>Get Started</button>
</Experiment>
);
}