From 75273b46f63237f2ca3a5f3602dbdfa0cab6c88d Mon Sep 17 00:00:00 2001 From: Minh Ha Do Date: Tue, 14 Jan 2025 09:55:43 +0100 Subject: [PATCH] Filter out amplitude cookies --- src/middleware.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/middleware.ts b/src/middleware.ts index 3a46b80a3..96768768f 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -55,6 +55,17 @@ function addSessionIdHeader(requestHeaders: Headers) { requestHeaders.set(SESSION_ID_TAG, getSessionId()); } +function filterAmplitudeCookies(requestHeaders: Headers) { + const cookies: string | null = requestHeaders.get("cookie"); + if (cookies) { + const filteredCookies = cookies + .split(";") + .filter((cookie: string) => !cookie.trim().startsWith("AMP_")) + .join("; "); + requestHeaders.set("cookie", filteredCookies); + } +} + const PUBLIC_FILE = /\.(.*)$/; // Due to limitations in the edge runtime, we can't use the prom-client library to track metrics directly here. @@ -87,6 +98,7 @@ export function middleware(request: NextRequest) { addCallIdHeader(requestHeaders); addSessionIdHeader(requestHeaders); + filterAmplitudeCookies(requestHeaders); const response = NextResponse.next({ request: {