Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more guidance to the Statsig example #970

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions edge-middleware/ab-testing-statsig/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export const config = {
matcher: '/',
}

// Manually sync as the SDK cannot poll for updates across requests since
// Edge Middleware does not allow for timers outside of the request handler
//
// See https://docs.statsig.com/integrations/vercel/#polling-for-updates-v5130
let lastSyncTime = 0
const syncInterval = 60_000 // 1 minute

export async function middleware(req: NextRequest, event: NextFetchEvent) {
// Get the user ID from the cookie or get a new one
let userId = req.cookies.get(UID_COOKIE)?.value
Expand All @@ -39,17 +46,25 @@ export async function middleware(req: NextRequest, event: NextFetchEvent) {
// 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,
})

// init within middleware so we get the actual time, as Date.now() will not
// return accourate time outside of the request lifecycle.
//
// Avoid syncing initially as we just initialized and will sync on the
// first request anyway.
if (lastSyncTime === 0) {
lastSyncTime = Date.now()
} else if (lastSyncTime < Date.now() - syncInterval) {
lastSyncTime = Date.now()
event.waitUntil(Statsig.syncConfigSpecs())
}

const experiment = Statsig.getExperimentWithExposureLoggingDisabledSync(
{ userID: userId },
EXPERIMENT
Expand Down