-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: always set session-id cookie in example middleware
- Loading branch information
Showing
1 changed file
with
35 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,43 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { UNLEASH_API_PROXY_DEFINITIONS, UNLEASH_COOKIE_NAME } from "./utils"; | ||
import { addBasePath } from "next/dist/client/add-base-path"; | ||
import { flagsClient, evaluateFlags, randomSessionId } from "@unleash/nextjs"; | ||
import { UNLEASH_API_PROXY_DEFINITIONS, UNLEASH_COOKIE_NAME } from "./utils"; | ||
|
||
export const config = { | ||
runtime: "experimental-edge", | ||
matcher: "/ab", | ||
runtime: "experimental-edge", | ||
}; | ||
|
||
export default async function middleware(req: NextRequest) { | ||
const sessionId = | ||
req.cookies.get(UNLEASH_COOKIE_NAME)?.value || randomSessionId(); | ||
|
||
const context = { sessionId }; // You can extend context with other server-side properties | ||
|
||
// Grab definitions from an endpoint cached on the edge | ||
const protocol = req.url.startsWith("https") ? "https://" : "http://"; | ||
const host = req.headers.get("host"); | ||
const endpoint = addBasePath(UNLEASH_API_PROXY_DEFINITIONS); | ||
const token = process.env.UNLEASH_RELAY_SECRET || ""; | ||
const definitionsUrl = `${protocol}${host}${endpoint}?token=${token}`; | ||
|
||
// Make a request to the edge-cached endpoint | ||
const definitions = await fetch(definitionsUrl).then((res) => res.json()); | ||
|
||
// Evaluate based on provided context | ||
const evaluated = await evaluateFlags(definitions, context); | ||
|
||
const variant = flagsClient(evaluated.toggles).getVariant("nextjs-example") | ||
?.payload?.value; | ||
|
||
const newUrl = req.nextUrl.clone(); | ||
// Redirect to variant | ||
newUrl.pathname = `/ab/${variant === "a" ? "a" : "b"}`; | ||
const res = NextResponse.rewrite(newUrl); | ||
res.cookies.set(UNLEASH_COOKIE_NAME, sessionId); | ||
|
||
return res; | ||
export async function middleware(req: NextRequest) { | ||
const sessionId = | ||
req.cookies.get(UNLEASH_COOKIE_NAME)?.value || `${Math.floor(Math.random() * 1_000_000_000)}`; | ||
let res: NextResponse; | ||
if (req.nextUrl.pathname.startsWith('/ab')) { | ||
const context = { sessionId }; // You can extend context with other server-side properties | ||
|
||
// Grab definitions from an endpoint cached on the edge | ||
const protocol = req.url.startsWith("https") ? "https://" : "http://"; | ||
const host = req.headers.get("host"); | ||
const endpoint = addBasePath(UNLEASH_API_PROXY_DEFINITIONS); | ||
const token = process.env.UNLEASH_RELAY_SECRET || ""; | ||
const definitionsUrl = `${protocol}${host}${endpoint}?token=${token}`; | ||
|
||
// Make a request to the edge-cached endpoint | ||
const definitions = await fetch(definitionsUrl).then((res) => res.json()); | ||
|
||
// Evaluate based on provided context | ||
const evaluated = await evaluateFlags(definitions, context); | ||
const variant = flagsClient(evaluated.toggles).getVariant("nextjs-example") | ||
?.payload?.value; | ||
|
||
const newUrl = req.nextUrl.clone(); | ||
// Redirect to variant | ||
newUrl.pathname = `/ab/${variant === "a" ? "a" : "b"}`; | ||
res = NextResponse.rewrite(newUrl); | ||
} | ||
else { | ||
res = NextResponse.next(); | ||
} | ||
res.cookies.set(UNLEASH_COOKIE_NAME, sessionId); | ||
return res; | ||
} | ||
|