-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.js
32 lines (27 loc) · 941 Bytes
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { DecisionMode, Flagship, LogLevel } from "./lib/flagship.jamstack";
import { NextResponse } from "next/server";
import BucketingData from "./bucketing.json";
const envId = "blvo2kijq6pg023l8ee0";
// This function can be marked `async` if using `await` inside
export async function middleware(request) {
const flagship = Flagship.start(envId, "api_key", {
fetchNow: false,
decisionMode: DecisionMode.BUCKETING,
initialBucketing: BucketingData,
logLevel: LogLevel.NONE,
});
// Create a new visitor
const visitor = flagship?.newVisitor({
visitorId: "my_visitor_id",
context: {},
});
// Fetch flags
await visitor?.fetchFlags();
const showBanner = visitor.getFlag("showBanner", false).getValue(false);
const url = new URL("/middleware-page", request.url);
url.search = `?showBanner=${showBanner}`;
return NextResponse.rewrite(url);
}
export const config = {
matcher: "/middleware",
};