The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Improve the way we cache clickIds
Steven Tey712a5ec17cc1
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.tsindex f8b77206c7..389d95a823 100644--- a/apps/web/app/api/track/click/route.ts+++ b/apps/web/app/api/track/click/route.ts@@ -49,18 +49,13 @@ export const POST = async (req: Request) => {clickId = nanoid(16);waitUntil(- Promise.allSettled([- redis.set(cacheKey, clickId, {- ex: 60 * 60,- }), // 1 hour- recordClick({- req,- clickId,- linkId: link.id,- url: link.url,- skipRatelimit: true,- }),- ]),+ recordClick({+ req,+ clickId,+ linkId: link.id,+ url: link.url,+ skipRatelimit: true,+ }),);}diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.tsindex f236ed5acf..c08e7137e2 100644--- a/apps/web/lib/tinybird/record-click.ts+++ b/apps/web/lib/tinybird/record-click.ts@@ -60,9 +60,13 @@ export async function recordClick({const cacheKey = `recordClick:${linkId}:${ip}`;if (!skipRatelimit) {- // deduplicate clicks from the same IP address + link ID – only record 1 click per hour+ // by default, we deduplicate clicks from the same IP address + link ID – only record 1 click per hour+ // here, we check if the clickId is cached in Redis within the last hour+ // if it is, and it's the same as the one that's passed in, then we skip recording the click+ // we need to do this to make it work with both server-side and client-side click tracking+ // @see: https://dub.co/docs/conversions/clicks/introductionconst cachedClickId = await redis.get<string>(cacheKey);- if (cachedClickId) {+ if (cachedClickId && cachedClickId === clickId) {return null;}}