demandsolution-codex / middleware.ts
Jacksuyu's picture
Fix anon identity in embedded Space with header fallback
374bf33
Raw History Blame Contribute Delete
635 Bytes
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const response = NextResponse.next();
const anon = request.cookies.get("anon_id")?.value;
if (!anon) {
response.cookies.set("anon_id", crypto.randomUUID(), {
httpOnly: true,
sameSite: process.env.NODE_ENV === "production" ? "none" : "lax",
secure: process.env.NODE_ENV === "production",
path: "/",
maxAge: 60 * 60 * 24 * 365,
});
}
return response;
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};