1. Configure Next.js with withTuskDrift
Wrap your Next.js configuration with the withTuskDrift function in your next.config.js or next.config.mjs file:
Basic Configuration
- CommonJS (next.config.js)
- ESM (next.config.mjs)
With Debug Logging for Next.js Integration
- CommonJS (next.config.js)
- ESM (next.config.mjs)
What withTuskDrift Does
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
Configuration Options
| 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. |
2. Create instrumentation file
Create aninstrumentation.ts (or .js) file at the root of your Next.js project (or inside the src folder if using one):
Initialization Parameters
| 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. |
3. Update package.json scripts
In yourpackage.json file, manually add a script for "dev:record" that allows you to start the service with Tusk Drift in record mode.
4. Update recording configurations
Update the.tusk/config.yaml file in your project root to include recording configurations.
If you’re testing Tusk Drift out locally for the first time, set sampling rate to 1.0. After testing, we recommend a sampling rate between 0.01-0.1.
Configuration Options
| 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. |
Troubleshooting
My packages aren't being instrumented
My packages aren't being instrumented
If logs are showing that 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.