The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
only mint clickId if not in redisFailOver mode
Steven Tey464ba8c09761
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.tsindex 41dac39d01..76411c76fb 100644--- a/apps/web/lib/middleware/link.ts+++ b/apps/web/lib/middleware/link.ts@@ -189,9 +189,10 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {const cookieStore = await cookies();let clickId = cookieStore.get(dubIdCookieName)?.value;- if (!clickId) {- // if we need to cache the clickId, check if clickId is cached in Redis (assuming no redisFailOver)- if (shouldCacheClickId && !redisFailOver) {+ // only mint a new clickId if not in Redis failover mode+ if (!clickId && !redisFailOver) {+ // if we need to cache the clickId, check if clickId is cached in Redis+ if (shouldCacheClickId) {const identityHash = await getIdentityHash(req);clickId =(await recordClickCache@@ -484,18 +485,20 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {isIosAppStoreUrl(ios) &&!req.nextUrl.searchParams.get("skip_deeplink_preview")) {- ev.waitUntil(- cacheDeepLinkClickData({- req,- clickId,- link: {- id: linkId,- domain,- key,- url, // pass the main destination URL to the cache (for deferred deep linking)- },- }),- );+ if (clickId) {+ ev.waitUntil(+ cacheDeepLinkClickData({+ req,+ clickId,+ link: {+ id: linkId,+ domain,+ key,+ url, // pass the main destination URL to the cache (for deferred deep linking)+ },+ }),+ );+ }// redirect to the deeplink interstitial splash page "DeepLinkPreviewPage"// we're doing this because the interstitial page needs to be on a different domain than the actual deep link domain@@ -552,18 +555,20 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {isGooglePlayStoreUrl(android) &&!req.nextUrl.searchParams.get("skip_deeplink_preview")) {- ev.waitUntil(- cacheDeepLinkClickData({- req,- clickId,- link: {- id: linkId,- domain,- key,- url, // pass the main destination URL to the cache (for deferred deep linking)- },- }),- );+ if (clickId) {+ ev.waitUntil(+ cacheDeepLinkClickData({+ req,+ clickId,+ link: {+ id: linkId,+ domain,+ key,+ url, // pass the main destination URL to the cache (for deferred deep linking)+ },+ }),+ );+ }// redirect to the deeplink interstitial splash page "DeepLinkPreviewPage"return createResponseWithCookies(diff --git a/apps/web/lib/middleware/utils/create-response-with-cookies.ts b/apps/web/lib/middleware/utils/create-response-with-cookies.tsindex 52133884ff..a957dfa79b 100644--- a/apps/web/lib/middleware/utils/create-response-with-cookies.ts+++ b/apps/web/lib/middleware/utils/create-response-with-cookies.ts@@ -10,16 +10,18 @@ export function createResponseWithCookies(}: {path: string;dubIdCookieName: string;- dubIdCookieValue: string;+ dubIdCookieValue?: string;dubTestUrlValue?: string | null;},): NextResponse {// 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 (dubIdCookieValue) {+ response.cookies.set(dubIdCookieName, dubIdCookieValue, {+ path,+ maxAge: 60 * 60, // 1 hour+ });+ }// set dub_test_url if this link has testVariants// caches for 1 week (for consistent user experience)