Skip to content

Commit

Permalink
add back skipRatelimit
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Feb 24, 2025
1 parent 6a98e09 commit d2b8214
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions apps/web/app/api/track/click/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ export const POST = withAxiom(
req,
clickId,
linkId: link.id,
domain,
key,
url: finalUrl,
workspaceId: link.projectId,
skipRatelimit: true,
...(referrer && { referrer }),
}),
);
Expand Down
13 changes: 8 additions & 5 deletions apps/web/app/api/track/visit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ export const POST = withAxiom(

const urlObj = new URL(url);

let linkKey = urlObj.pathname.slice(1);
if (linkKey === "") {
linkKey = "_root";
let key = urlObj.pathname.slice(1);
if (key === "") {
key = "_root";
}

const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
const cacheKey = `recordClick:${domain}:${linkKey}:${ip}`;
const cacheKey = `recordClick:${domain}:${key}:${ip}`;

let clickId = await redis.get<string>(cacheKey);

// only generate + record a new click ID if it's not already cached in Redis
if (!clickId) {
clickId = nanoid(16);

let link = await getLinkWithAllowedHostnames(domain, linkKey);
let link = await getLinkWithAllowedHostnames(domain, key);

if (!link) {
return NextResponse.json(
Expand All @@ -69,8 +69,11 @@ export const POST = withAxiom(
req,
clickId,
linkId: link.id,
domain,
key,
url: finalUrl,
workspaceId: link.projectId,
skipRatelimit: true,
...(referrer && { referrer }),
}),
);
Expand Down
6 changes: 4 additions & 2 deletions apps/web/lib/tinybird/record-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export async function recordClick({
url,
webhookIds,
workspaceId,
skipRatelimit,
timestamp,
referrer,
}: {
Expand All @@ -44,6 +45,7 @@ export async function recordClick({
url?: string;
webhookIds?: string[];
workspaceId: string | undefined;
skipRatelimit?: boolean;
timestamp?: string;
referrer?: string;
}) {
Expand All @@ -66,8 +68,8 @@ export async function recordClick({
const cacheKey = `recordClick:${domain}:${key}:${ip}`;

// by default, we deduplicate clicks for a domain + key pair from the same IP address – only record 1 click per hour
// we only need to do these if domain + key are defined (only in middleware/link and not /api/track/:path endpoints)
if (domain && key) {
// we only need to do these if skipRatelimit is not true (we skip it in /api/track/:path endpoints)
if (!skipRatelimit) {
// here, we check if the clickId is cached in Redis within the last hour
const cachedClickId = await redis.get<string>(cacheKey);
if (cachedClickId) {
Expand Down

0 comments on commit d2b8214

Please sign in to comment.