Skip to content

Commit

Permalink
fix statsig usage (#911)
Browse files Browse the repository at this point in the history
Prevent statsig from fetching in middleware.

- Turning on `localMode` will log an error on any network activity
- Setting `initStrategyForIDLists: "never"` will prevent Statsig from
attempting to fetch the ID List over the network, as ID Lists are not
supported by the Edge Config Adapter (yet)
  • Loading branch information
dferber90 committed May 8, 2024
1 parent c140da0 commit 62c700e
Show file tree
Hide file tree
Showing 3 changed files with 533 additions and 442 deletions.
33 changes: 29 additions & 4 deletions edge-middleware/ab-testing-statsig/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse, NextFetchEvent } from 'next/server'
import Statsig from 'statsig-node'
import Statsig from 'statsig-node-lite'
import { EdgeConfigDataAdapter } from 'statsig-node-vercel'
import { createClient } from '@vercel/edge-config'
import { EXPERIMENT, UID_COOKIE, GROUP_PARAM_FALLBACK } from './lib/constants'
Expand Down Expand Up @@ -28,9 +28,32 @@ export async function middleware(req: NextRequest, event: NextFetchEvent) {
hasUserId = false
}

await Statsig.initialize(process.env.STATSIG_SERVER_API_KEY!, { dataAdapter })
await Statsig.initialize(process.env.STATSIG_SERVER_API_KEY!, {
// 🚨 It's extremly important to set this, otherwise Statsig will attempt
// to fetch the ID List over the network, which is slow and would render
// using the Edge Config Adapter useless.
//
// If you are not using the ID List feature, set this to "none".
//
// Otherwise consider setting it to "lazy" but be aware of the consequences
// that the ID List will not apply in most cases as it will only get fetched
// after the experiment ran
initStrategyForIDLists: 'none',
// 🚨 This setting will prevent Statsig from making any network requests,
// thus ensuring middleware stays extremly fast.
localMode: true,
// This makes Statsig load experiments from Edge Config
dataAdapter,
// Disable any syncing to prevent network activity, as Edge Config will
// return the latest values anyhow, and as ID Lists are disabled.
disableIdListsSync: true,
disableRulesetsSync: true,
})

const experiment = await Statsig.getExperiment({ userID: userId }, EXPERIMENT)
const experiment = Statsig.getExperimentWithExposureLoggingDisabledSync(
{ userID: userId },
EXPERIMENT
)
const bucket = experiment.get<string>('bucket', GROUP_PARAM_FALLBACK)

// Clone the URL and change its pathname to point to a bucket
Expand All @@ -48,7 +71,9 @@ export async function middleware(req: NextRequest, event: NextFetchEvent) {
}

// Flush exposure logs to Statsig
event.waitUntil(Statsig.flush())
// Disabled as we activated localMode. Turn localMode off if you need to log
// exposures from here.
// event.waitUntil(Statsig.flush())

return res
}
4 changes: 2 additions & 2 deletions edge-middleware/ab-testing-statsig/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"next": "canary",
"react": "latest",
"react-dom": "latest",
"statsig-node": "^5.9.3",
"statsig-node-vercel": "^0.2.0",
"statsig-node-lite": "0.2.0",
"statsig-node-vercel": "0.2.0",
"statsig-react": "^1.30.3"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 62c700e

Please sign in to comment.