Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update cookie name to include the domain and key

Kiran Ka5528b3041f1
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 3c5f02179b..034868e1a7 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -207,14 +207,22 @@ export default async function LinkMiddleware(
}
}
- let clickId;
- // for links with trackConversion enabled, always check
- if (trackConversion) {
- const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
- clickId = (await clickCache.get({ domain, key, ip })) || undefined;
- } else {
- const cookieStore = cookies();
- clickId = cookieStore.get("dub_id")?.value;
+ const cookieName = `dub_id_${domain}_${key}`;
+
+ const cookieStore = cookies();
+ let clickId = cookieStore.get(cookieName)?.value;
+ if (!clickId) {
+ // if trackConversion is enabled, check if clickId is cached in Redis
+ if (trackConversion) {
+ const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
+
+ clickId = (await clickCache.get({ domain, key, ip })) || undefined;
+ }
+
+ // if there's still no clickId, generate a new one
+ if (!clickId) {
+ clickId = nanoid(16);
+ }
}
// if there's still no clickId, generate a new one
@@ -222,6 +230,12 @@ export default async function LinkMiddleware(
clickId = nanoid(16);
}
+ const cookieData = {
+ name: cookieName,
+ value: clickId,
+ path: `/${originalKey}`,
+ };
+
// for root domain links, if there's no destination URL, rewrite to placeholder page
if (!url) {
ev.waitUntil(
@@ -246,7 +260,7 @@ export default async function LinkMiddleware(
...(shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
},
}),
- { clickId, path: `/${originalKey}` },
+ cookieData,
);
}
@@ -267,7 +281,7 @@ export default async function LinkMiddleware(
},
},
),
- { clickId, path: `/${originalKey}` },
+ cookieData,
);
// rewrite to deeplink page if the link is a mailto: or tel:
@@ -304,7 +318,7 @@ export default async function LinkMiddleware(
},
},
),
- { clickId, path: `/${originalKey}` },
+ cookieData,
);
// rewrite to target URL if link cloaking is enabled
@@ -343,7 +357,7 @@ export default async function LinkMiddleware(
},
},
),
- { clickId, path: `/${originalKey}` },
+ cookieData,
);
// redirect to iOS link if it is specified and the user is on an iOS device
@@ -376,7 +390,7 @@ export default async function LinkMiddleware(
status: key === "_root" ? 301 : 302,
},
),
- { clickId, path: `/${originalKey}` },
+ cookieData,
);
// redirect to Android link if it is specified and the user is on an Android device
@@ -409,7 +423,7 @@ export default async function LinkMiddleware(
status: key === "_root" ? 301 : 302,
},
),
- { clickId, path: `/${originalKey}` },
+ cookieData,
);
// redirect to geo-specific link if it is specified and the user is in the specified country
@@ -442,7 +456,7 @@ export default async function LinkMiddleware(
status: key === "_root" ? 301 : 302,
},
),
- { clickId, path: `/${originalKey}` },
+ cookieData,
);
// regular redirect
@@ -488,7 +502,7 @@ export default async function LinkMiddleware(
status: key === "_root" ? 301 : 302,
},
),
- { clickId, path: `/${originalKey}` },
+ cookieData,
);
}
}
diff --git a/apps/web/lib/middleware/utils/create-response-with-cookie.ts b/apps/web/lib/middleware/utils/create-response-with-cookie.ts
index 55e90bf495..d8deaf2272 100644
--- a/apps/web/lib/middleware/utils/create-response-with-cookie.ts
+++ b/apps/web/lib/middleware/utils/create-response-with-cookie.ts
@@ -2,9 +2,17 @@ import { NextResponse } from "next/server";
export function createResponseWithCookie(
response: NextResponse,
- { clickId, path }: { clickId: string; path: string },
-): NextResponse {
- response.cookies.set("dub_id", clickId, {
+ {
+ name,
+ value,
+ path,
+ }: {
+ name: string;
+ value: string;
+ path: string;
+ },
+) {
+ response.cookies.set(name, value, {
path,
maxAge: 60 * 60, // 1 hour
});