Next.js

circle-info

You can integrate Hackle with Next.js using the @hackler/react-sdk package.

For detailed integration and usage guidance for the Hackle SDK, refer to the React guide.

1. Installation

To use Hackle in Next.js, you need to install @hackler/react-sdk.

npm install @hackler/react-sdk
circle-info

Add the Hackle SDK key to your .env.development and .env.production files for the respective Development and Production environments.

NEXT_PUBLIC_HACKLE_SDK_KEY=your-hackle-sdk-key

2. Integration

Hackle supports both Page Routerarrow-up-right and App Routerarrow-up-right in Next.js. There are some differences in how to integrate Hackle with each.

Creating an Instance

// app/hackleClient.client.ts

"use client";

import { createInstance } from "@hackler/react-sdk";

export const hackleClient = createInstance(
  process.env.NEXT_PUBLIC_HACKLE_SDK_KEY!
);

App Router - Creating a Provider

App Router - Root Layout Setup

Page Router - Using a Provider

3. Key Features

A/B Test Variation Distribution

Feature Flag Decision

Remote Config

Event Tracking

Advanced Configuration

Server-side variation distribution in Next.js requires additional setup. When distributing on the server side, you need to carefully manage the user identifier and distinguish between server-side and client-side code.

Installation

You need to separately install the SDK for use in the server-side (Node.js) environment.

Adding the SDK Key

Add the Hackle SDK key to your .env.development and .env.production files for the respective Development and Production environments.

You can find the Server key at https://dashboard.hackle.io/config/sdk-settingarrow-up-right.

Integration

Server-Side Usage

A/B Test Variation Distribution

Feature Flag Decision

Remote Config

Event Tracking

instrumentation

HackleClient fetches configuration from the Hackle server during initialization and periodically synchronizes afterward. Using instrumentation.ts allows you to keep your code simpler without calling await hackleClient.onInitialized() on every page. This is recommended for Next.js v15 and above, where instrumentationHook is enabled by default.

Last updated