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.

createResponseWithCookies

Steven Tey06babfcdab25
diff --git a/apps/web/lib/middleware/utils/create-response-with-cookie.ts b/apps/web/lib/middleware/utils/create-response-with-cookie.ts
deleted file mode 100644
index d8deaf2272..0000000000
--- a/apps/web/lib/middleware/utils/create-response-with-cookie.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-import { NextResponse } from "next/server";
-
-export function createResponseWithCookie(
- response: NextResponse,
- {
- name,
- value,
- path,
- }: {
- name: string;
- value: string;
- path: string;
- },
-) {
- response.cookies.set(name, value, {
- path,
- maxAge: 60 * 60, // 1 hour
- });
-
- return response;
-}
diff --git a/apps/web/lib/middleware/utils/create-response-with-cookies.ts b/apps/web/lib/middleware/utils/create-response-with-cookies.ts
index 44eb93c310..52133884ff 100644
--- a/apps/web/lib/middleware/utils/create-response-with-cookies.ts
+++ b/apps/web/lib/middleware/utils/create-response-with-cookies.ts
@@ -4,21 +4,29 @@ export function createResponseWithCookies(
response: NextResponse,
{
path,
- clickId,
- testUrl,
- }: { path: string; clickId?: string; testUrl?: string | null },
+ dubIdCookieName,
+ dubIdCookieValue,
+ dubTestUrlValue,
+ }: {
+ path: string;
+ dubIdCookieName: string;
+ dubIdCookieValue: string;
+ dubTestUrlValue?: string | null;
+ },
): NextResponse {
- if (clickId !== undefined) {
- response.cookies.set("dub_id", clickId, {
- path,
- maxAge: 60 * 60, // 1 hour
- });
- }
+ // set dub_id_<domain>_<key> cookie
+ // this caches dub_id for 1 hour (for deduplication)
+ response.cookies.set(dubIdCookieName, dubIdCookieValue, {
+ path,
+ maxAge: 60 * 60, // 1 hour
+ });
- if (testUrl) {
- response.cookies.set("dub_test_url", testUrl, {
+ // set dub_test_url if this link has testVariants
+ // caches for 1 week (for consistent user experience)
+ if (dubTestUrlValue) {
+ response.cookies.set("dub_test_url", dubTestUrlValue, {
path,
- maxAge: 60 * 60, // 1 hour
+ maxAge: 60 * 60 * 24 * 7, // 1 week
});
}