The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
log /track/click in axiom
Steven Tey67c0588b1c6f
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.tsindex 421c1e0192..fb2b86ecbd 100644--- a/apps/web/app/api/track/click/route.ts+++ b/apps/web/app/api/track/click/route.ts@@ -5,6 +5,7 @@ import { recordClick } from "@/lib/tinybird";import { ratelimit, redis } from "@/lib/upstash";import { isValidUrl, LOCALHOST_IP, nanoid, punyEncode } from "@dub/utils";import { ipAddress, waitUntil } from "@vercel/functions";+import { AxiomRequest, withAxiom } from "next-axiom";import { NextResponse } from "next/server";export const runtime = "edge";@@ -16,101 +17,106 @@ const CORS_HEADERS = {};// POST /api/track/click – Track a click event from client side-export const POST = async (req: Request) => {- try {- const { domain, key, url } = 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;+export const POST = withAxiom(+ async (req: AxiomRequest) => {+ try {+ const { domain, key, url } = await parseRequestBody(req);++ if (!domain || !key) {+ throw new DubApiError({+ code: "bad_request",+ message: "Missing domain or key",+ });+ }- const { success } = await ratelimit().limit(- `track-click:${domain}-${key}:${ip}`,- );+ const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;- if (!success) {- throw new DubApiError({- code: "rate_limit_exceeded",- message: "Don't DDoS me pls 🥺",- });- }+ const { success } = await ratelimit().limit(+ `track-click:${domain}-${key}:${ip}`,+ );- const { rows } = await conn.execute<{- id: string;- url: string;- projectId: string;- allowedHostnames: string[];- }>(- "SELECT Link.id, Link.url, projectId, allowedHostnames FROM Link LEFT JOIN Project ON Link.projectId = Project.id WHERE domain = ? AND `key` = ?",- [domain, punyEncode(decodeURIComponent(key))],- );-- const link =- rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;-- if (!link) {- return new Response(null, {- status: 204,- headers: CORS_HEADERS,- });- }+ if (!success) {+ throw new DubApiError({+ code: "rate_limit_exceeded",+ message: "Don't DDoS me pls 🥺",+ });+ }- const allowedHostnames = link.allowedHostnames;+ const { rows } = await conn.execute<{+ id: string;+ url: string;+ projectId: string;+ allowedHostnames: string[];+ }>(+ "SELECT Link.id, Link.url, projectId, allowedHostnames FROM Link LEFT JOIN Project ON Link.projectId = Project.id WHERE domain = ? AND `key` = ?",+ [domain, punyEncode(decodeURIComponent(key))],+ );- if (allowedHostnames && allowedHostnames.length > 0) {- const source = req.headers.get("referer") || req.headers.get("origin");- const sourceUrl = source ? new URL(source) : null;- const hostname = sourceUrl?.hostname.replace(/^www\./, "");-- if (!hostname || !allowedHostnames.includes(hostname)) {- console.error("Hostname not allowed.", {- hostname,- allowedHostnames,- });+ const link =+ rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;+ if (!link) {return new Response(null, {status: 204,headers: CORS_HEADERS,});}- }- const cacheKey = `recordClick:${link.id}:${ip}`;- let clickId = await redis.get<string>(cacheKey);+ const allowedHostnames = link.allowedHostnames;++ if (allowedHostnames && allowedHostnames.length > 0) {+ const source = req.headers.get("referer") || req.headers.get("origin");+ const sourceUrl = source ? new URL(source) : null;+ const hostname = sourceUrl?.hostname.replace(/^www\./, "");++ if (!hostname || !allowedHostnames.includes(hostname)) {+ console.error("Hostname not allowed.", {+ hostname,+ allowedHostnames,+ });++ return new Response(null, {+ status: 204,+ headers: CORS_HEADERS,+ });+ }+ }- if (!clickId) {- clickId = nanoid(16);- const finalUrl = isValidUrl(url) ? url : link.url;+ const cacheKey = `recordClick:${link.id}:${ip}`;+ let clickId = await redis.get<string>(cacheKey);++ if (!clickId) {+ clickId = nanoid(16);+ const finalUrl = isValidUrl(url) ? url : link.url;++ waitUntil(+ recordClick({+ req,+ clickId,+ linkId: link.id,+ url: finalUrl,+ skipRatelimit: true,+ workspaceId: link.projectId,+ }),+ );+ }- waitUntil(- recordClick({- req,+ return NextResponse.json(+ {clickId,- linkId: link.id,- url: finalUrl,- skipRatelimit: true,- workspaceId: link.projectId,- }),+ },+ {+ headers: CORS_HEADERS,+ },);+ } catch (error) {+ return handleAndReturnErrorResponse(error, CORS_HEADERS);}-- return NextResponse.json(- {- clickId,- },- {- headers: CORS_HEADERS,- },- );- } catch (error) {- return handleAndReturnErrorResponse(error, CORS_HEADERS);- }-};+ },+ {+ logRequestDetails: ["body", "nextUrl"],+ },+);export const OPTIONS = () => {return new Response(null, {