This guide explains how to set up Tusk Drift in your Next.js application.
Wrap your Next.js configuration with the withTuskDrift function in your next.config.js or next.config.ts file:
// next.config.js
const { withTuskDrift } = require("@use-tusk/drift-node-sdk/next");
module.exports = withTuskDrift({
// Your Next.js config
});// next.config.mjs
import { withTuskDrift } from "@use-tusk/drift-node-sdk/next";
export default withTuskDrift({
// Your Next.js config
});// next.config.js
const { withTuskDrift } = require("@use-tusk/drift-node-sdk/next");
module.exports = withTuskDrift(
{
// Your Next.js config
},
{
// Tusk Drift options
debug: true, // Enable debug logging
},
);// next.config.mjs
import { withTuskDrift } from "@use-tusk/drift-node-sdk/next";
export default withTuskDrift(
{
// Your Next.js config
},
{
// Tusk Drift options
debug: true, // Enable debug logging
},
);The withTuskDrift wrapper automatically:
- ✅ Enables the Next.js instrumentation hook (for Next.js < 15.0.0-rc.1)
- ✅ Configures webpack externals for proper module interception
- ✅ Detects your Next.js version and adjusts configuration accordingly
- ✅ Preserves your existing Next.js configuration
| Option | Type | Default | Description |
|---|---|---|---|
debug |
boolean |
false |
Enable debug logging to see what Tusk Drift is configuring during build. |
disableInstrumentationHook |
boolean |
false |
Disable automatic setting of experimental.instrumentationHook. Not recommended, will break Tusk Drift's Next.js integration. |
suppressWarnings |
boolean |
false |
Suppress all warnings from Tusk Drift's Next.js integration. |
Create an instrumentation.ts (or .js) file at the root of your Next.js project (or inside the src folder if using one):
// instrumentation.ts
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
const { TuskDrift } = await import("@use-tusk/drift-node-sdk");
TuskDrift.initialize({
apiKey: process.env.TUSK_API_KEY,
env: process.env.NODE_ENV,
logLevel: "debug",
});
// Mark app as ready immediately
TuskDrift.markAppAsReady();
}
}More context on setting up instrumentations for Next.js apps can be found here.
| Option | Type | Default | Description |
|---|---|---|---|
apiKey |
string |
Required if using Tusk Cloud | Your Tusk Drift API key from the dashboard. |
env |
string |
process.env.NODE_ENV |
The environment name (e.g., 'dev', 'staging', 'production'). |
logLevel |
'silent' | 'error' | 'warn' | 'info' | 'debug' |
'info' |
The logging level for the Tusk Drift SDK. |
samplingRate |
number |
1.0 |
Override sampling rate (0.0 - 1.0) for recording. Takes precedence over TUSK_SAMPLING_RATE env var and config file. |
Update your package.json scripts:
{
"scripts": {
"dev": "next dev",
"dev:record": "TUSK_DRIFT_MODE=RECORD next dev"
}
}The sampling rate determines what percentage of requests are recorded during replay tests. Tusk Drift supports three ways to configure the sampling rate, with the following precedence (highest to lowest):
- Init Parameter
- Environment Variable (
TUSK_SAMPLING_RATE) - Configuration File (
.tusk/config.yaml)
If not specified, the default sampling rate is 1.0 (100%).
Set the sampling rate directly in your initialization code:
// instrumentation.ts
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
const { TuskDrift } = await import("@use-tusk/drift-node-sdk");
TuskDrift.initialize({
apiKey: process.env.TUSK_API_KEY,
env: process.env.NODE_ENV,
samplingRate: 0.1, // 10% of requests
});
TuskDrift.markAppAsReady();
}
}Set the TUSK_SAMPLING_RATE environment variable:
# Development - record everything
TUSK_SAMPLING_RATE=1.0 npm run dev
# Production - sample 10% of requests
TUSK_SAMPLING_RATE=0.1 npm startUpdate the .tusk/config.yaml file in your project root to include recording configuration:
# ... existing configuration ...
recording:
sampling_rate: 0.1
export_spans: true
enable_env_var_recording: true| Option | Type | Default | Description |
|---|---|---|---|
sampling_rate |
number |
1.0 |
The sampling rate (0.0 - 1.0). 1.0 means 100% of requests are recorded, 0.0 means no requests are recorded. |
export_spans |
boolean |
false |
Whether to export spans to the Tusk backend. If false, spans are only saved locally in .tusk/traces. |
enable_env_var_recording |
boolean |
false |
Whether to record and replay environment variables. Recommended for accurate replay behavior if your app's logic depends on environment variables. |
If your packages aren't being instrumented:
-
Check file location: Ensure
instrumentation.tsis at the project root (same level asnext.config.js), not in theapporpagesdirectory. -
Check runtime guard: Make sure you have the
NEXT_RUNTIME === 'nodejs'check in yourregister()function. -
Enable debug logging: Set
debug: truein yourwithTuskDriftoptions to see what's being configured.