@@ -17,6 +17,13 @@ export const config = {
17
17
matcher : '/' ,
18
18
}
19
19
20
+ // Manually sync as the SDK cannot poll for updates across requests since
21
+ // Edge Middleware does not allow for timers outside of the request handler
22
+ //
23
+ // See https://docs.statsig.com/integrations/vercel/#polling-for-updates-v5130
24
+ let lastSyncTime = 0
25
+ const syncInterval = 60_000 // 1 minute
26
+
20
27
export async function middleware ( req : NextRequest , event : NextFetchEvent ) {
21
28
// Get the user ID from the cookie or get a new one
22
29
let userId = req . cookies . get ( UID_COOKIE ) ?. value
@@ -39,17 +46,25 @@ export async function middleware(req: NextRequest, event: NextFetchEvent) {
39
46
// that the ID List will not apply in most cases as it will only get fetched
40
47
// after the experiment ran
41
48
initStrategyForIDLists : 'none' ,
42
- // 🚨 This setting will prevent Statsig from making any network requests,
43
- // thus ensuring middleware stays extremly fast.
44
- localMode : true ,
45
49
// This makes Statsig load experiments from Edge Config
46
50
dataAdapter,
47
51
// Disable any syncing to prevent network activity, as Edge Config will
48
52
// return the latest values anyhow, and as ID Lists are disabled.
49
53
disableIdListsSync : true ,
50
- disableRulesetsSync : true ,
51
54
} )
52
55
56
+ // init within middleware so we get the actual time, as Date.now() will not
57
+ // return accourate time outside of the request lifecycle.
58
+ //
59
+ // Avoid syncing initially as we just initialized and will sync on the
60
+ // first request anyway.
61
+ if ( lastSyncTime === 0 ) {
62
+ lastSyncTime = Date . now ( )
63
+ } else if ( lastSyncTime < Date . now ( ) - syncInterval ) {
64
+ lastSyncTime = Date . now ( )
65
+ event . waitUntil ( Statsig . syncConfigSpecs ( ) )
66
+ }
67
+
53
68
const experiment = Statsig . getExperimentWithExposureLoggingDisabledSync (
54
69
{ userID : userId } ,
55
70
EXPERIMENT
0 commit comments