@@ -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}
0 commit comments