Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Merge branch 'main' into click-cache-class

Steven Tey41c1b8e42ace
diff --cc apps/web/app/api/track/click/route.ts
index 3d17106238,7926039f3b..efb9e754bf
--- a/apps/web/app/api/track/click/route.ts
+++ b/apps/web/app/api/track/click/route.ts
@@@ -18,72 -18,68 +18,69 @@@ const CORS_HEADERS =
};
// POST /api/track/click – Track a click event from the client-side
- export const POST = withAxiom(
- async (req: AxiomRequest) => {
- try {
- const { domain, key, url, referrer } = await parseRequestBody(req);
+ export const POST = withAxiom(async (req: AxiomRequest) => {
+ try {
+ const { domain, key, url, referrer } = await parseRequestBody(req);
- if (!domain || !key) {
- throw new DubApiError({
- code: "bad_request",
- message: "Missing domain or key",
- });
- }
-
- const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
+ if (!domain || !key) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "Missing domain or key",
+ });
+ }
- let clickId = await clickCache.get({ domain, key, ip });
+ const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
- // only generate + record a new click ID if it's not already cached in Redis
- if (!clickId) {
- clickId = nanoid(16);
+ const cacheKey = `recordClick:${domain}:${key}:${ip}`;
+ let clickId = await redis.get<string>(cacheKey);
- const link = await getLinkWithAllowedHostnames(domain, key);
+ // only generate + record a new click ID if it's not already cached in Redis
+ if (!clickId) {
+ clickId = nanoid(16);
- if (!link) {
- throw new DubApiError({
- code: "not_found",
- message: `Link not found for domain: ${domain} and key: ${key}.`,
- });
- }
+ const link = await getLinkWithAllowedHostnames(domain, key);
- const allowedHostnames = link.allowedHostnames;
- verifyAnalyticsAllowedHostnames({ allowedHostnames, req });
+ if (!link) {
+ throw new DubApiError({
+ code: "not_found",
+ message: `Link not found for domain: ${domain} and key: ${key}.`,
+ });
+ }
- const finalUrl = isValidUrl(url) ? url : link.url;
+ const allowedHostnames = link.allowedHostnames;
+ verifyAnalyticsAllowedHostnames({ allowedHostnames, req });
- waitUntil(
- recordClick({
- req,
- clickId,
- linkId: link.id,
- domain,
- key,
- url: finalUrl,
- workspaceId: link.projectId,
- skipRatelimit: true,
- ...(referrer && { referrer }),
- trackConversion: link.trackConversion,
- }),
- );
- }
++ let clickId = await clickCache.get({ domain, key, ip });
+ const finalUrl = isValidUrl(url) ? url : link.url;
- return NextResponse.json(
- {
+ waitUntil(
+ recordClick({
+ req,
clickId,
- },
- {
- headers: CORS_HEADERS,
- },
+ linkId: link.id,
+ domain,
+ key,
+ url: finalUrl,
+ workspaceId: link.projectId,
+ skipRatelimit: true,
+ ...(referrer && { referrer }),
+ trackConversion: link.trackConversion,
+ }),
);
- } catch (error) {
- return handleAndReturnErrorResponse(error, CORS_HEADERS);
}
- },
- {
- logRequestDetails: ["nextUrl"],
- },
- );
+
+ return NextResponse.json(
+ {
+ clickId,
+ },
+ {
+ headers: CORS_HEADERS,
+ },
+ );
+ } catch (error) {
+ return handleAndReturnErrorResponse(error, CORS_HEADERS);
+ }
+ });
export const OPTIONS = () => {
return new Response(null, {
diff --cc apps/web/app/api/track/visit/route.ts
index 9157216097,16d38f9518..56a87fb196
--- a/apps/web/app/api/track/visit/route.ts
+++ b/apps/web/app/api/track/visit/route.ts
@@@ -18,63 -18,43 +18,42 @@@ const CORS_HEADERS =
};
// POST /api/track/visit – Track a visit event from the client-side
- export const POST = withAxiom(
- async (req: AxiomRequest) => {
- try {
- const { domain, url, referrer } = await parseRequestBody(req);
-
- if (!domain || !url) {
- throw new DubApiError({
- code: "bad_request",
- message: "Missing domain or url",
- });
- }
+ export const POST = withAxiom(async (req: AxiomRequest) => {
+ try {
+ const { domain, url, referrer } = await parseRequestBody(req);
+
+ if (!domain || !url) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "Missing domain or url",
+ });
+ }
- const urlObj = new URL(url);
+ const urlObj = new URL(url);
- let key = urlObj.pathname.slice(1);
- if (key === "") {
- key = "_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}:${key}:${ip}`;
+
- let clickId = await redis.get<string>(cacheKey);
++ let clickId = await clickCache.get({ domain, key, ip });
- const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
-
- let clickId = await clickCache.get({ domain, key, ip });
-
- // 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, key);
-
- if (!link) {
- return NextResponse.json(
- {
- clickId: null,
- },
- {
- headers: CORS_HEADERS,
- },
- );
- }
-
- const allowedHostnames = link.allowedHostnames;
- verifyAnalyticsAllowedHostnames({ allowedHostnames, req });
-
- const finalUrl = isValidUrl(url) ? url : link.url;
-
- waitUntil(
- recordClick({
- req,
- clickId,
- linkId: link.id,
- domain,
- key,
- url: finalUrl,
- workspaceId: link.projectId,
- skipRatelimit: true,
- ...(referrer && { referrer }),
- }),
+ // 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, key);
+
+ if (!link) {
+ return NextResponse.json(
+ {
+ clickId: null,
+ },
+ {
+ headers: CORS_HEADERS,
+ },
);
}