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

feat: always set session-id cookie in example middleware #74

Merged
merged 2 commits into from
May 31, 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
67 changes: 35 additions & 32 deletions example/src/middleware.ts
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 || randomSessionId();
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;
}

Loading