Skip to content

Commit cb8d9ea

Browse files
authored
Merge pull request dubinc#2259 from dubinc/fix-click-id
Fix clickId
2 parents 84b93a2 + 61b0d1a commit cb8d9ea

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

apps/web/lib/middleware/link.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,30 @@ export default async function LinkMiddleware(
207207
}
208208
}
209209

210+
const cookieName = `dub_id_${domain}_${key}`;
211+
210212
const cookieStore = cookies();
211-
let clickId = cookieStore.get("dub_id")?.value;
213+
let clickId = cookieStore.get(cookieName)?.value;
212214
if (!clickId) {
213215
// if trackConversion is enabled, check if clickId is cached in Redis
214216
if (trackConversion) {
215217
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
216218

217219
clickId = (await clickCache.get({ domain, key, ip })) || undefined;
218220
}
221+
219222
// if there's still no clickId, generate a new one
220223
if (!clickId) {
221224
clickId = nanoid(16);
222225
}
223226
}
224227

228+
const cookieData = {
229+
name: cookieName,
230+
value: clickId,
231+
path: `/${originalKey}`,
232+
};
233+
225234
// for root domain links, if there's no destination URL, rewrite to placeholder page
226235
if (!url) {
227236
ev.waitUntil(
@@ -246,7 +255,7 @@ export default async function LinkMiddleware(
246255
...(shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
247256
},
248257
}),
249-
{ clickId, path: `/${originalKey}` },
258+
cookieData,
250259
);
251260
}
252261

@@ -267,7 +276,7 @@ export default async function LinkMiddleware(
267276
},
268277
},
269278
),
270-
{ clickId, path: `/${originalKey}` },
279+
cookieData,
271280
);
272281

273282
// rewrite to deeplink page if the link is a mailto: or tel:
@@ -304,7 +313,7 @@ export default async function LinkMiddleware(
304313
},
305314
},
306315
),
307-
{ clickId, path: `/${originalKey}` },
316+
cookieData,
308317
);
309318

310319
// rewrite to target URL if link cloaking is enabled
@@ -343,7 +352,7 @@ export default async function LinkMiddleware(
343352
},
344353
},
345354
),
346-
{ clickId, path: `/${originalKey}` },
355+
cookieData,
347356
);
348357

349358
// redirect to iOS link if it is specified and the user is on an iOS device
@@ -376,7 +385,7 @@ export default async function LinkMiddleware(
376385
status: key === "_root" ? 301 : 302,
377386
},
378387
),
379-
{ clickId, path: `/${originalKey}` },
388+
cookieData,
380389
);
381390

382391
// redirect to Android link if it is specified and the user is on an Android device
@@ -409,7 +418,7 @@ export default async function LinkMiddleware(
409418
status: key === "_root" ? 301 : 302,
410419
},
411420
),
412-
{ clickId, path: `/${originalKey}` },
421+
cookieData,
413422
);
414423

415424
// redirect to geo-specific link if it is specified and the user is in the specified country
@@ -442,7 +451,7 @@ export default async function LinkMiddleware(
442451
status: key === "_root" ? 301 : 302,
443452
},
444453
),
445-
{ clickId, path: `/${originalKey}` },
454+
cookieData,
446455
);
447456

448457
// regular redirect
@@ -488,7 +497,7 @@ export default async function LinkMiddleware(
488497
status: key === "_root" ? 301 : 302,
489498
},
490499
),
491-
{ clickId, path: `/${originalKey}` },
500+
cookieData,
492501
);
493502
}
494503
}

apps/web/lib/middleware/utils/create-response-with-cookie.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ import { NextResponse } from "next/server";
22

33
export function createResponseWithCookie(
44
response: NextResponse,
5-
{ clickId, path }: { clickId: string; path: string },
6-
): NextResponse {
7-
response.cookies.set("dub_id", clickId, {
5+
{
6+
name,
7+
value,
8+
path,
9+
}: {
10+
name: string;
11+
value: string;
12+
path: string;
13+
},
14+
) {
15+
response.cookies.set(name, value, {
816
path,
917
maxAge: 60 * 60, // 1 hour
1018
});

0 commit comments

Comments
 (0)