Next.js
1. Installation
To use Hackle in Next.js, you need to install @hackler/react-sdk.
npm install @hackler/react-sdkyarn add @hackler/react-sdkpnpm add @hackler/react-sdkNEXT_PUBLIC_HACKLE_SDK_KEY=your-hackle-sdk-key2. Integration
Hackle supports both Page Router and App Router 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/hackleClient.client.ts
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
As of React SDK 12.0.0, the user prop of HackleProvider is applied only when its value changes, and when applied it replaces the existing user information instead of merging it.
If you use both the user prop and the user information setter functions (setUser, setUserId, setDeviceId, and so on), user information can be lost. Use only one of the two approaches.
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-setting.
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