The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
getLinkWithAllowedHostnames
Steven Tey3e062242bf8d
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.tsindex b99740c579..015d49fead 100644--- a/apps/web/app/api/track/click/route.ts+++ b/apps/web/app/api/track/click/route.ts@@ -1,10 +1,10 @@import { verifyAnalyticsAllowedHostnames } from "@/lib/analytics/verify-analytics-allowed-hostnames";import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";import { parseRequestBody } from "@/lib/api/utils";-import { conn } from "@/lib/planetscale/connection";+import { getLinkWithAllowedHostnames } from "@/lib/planetscale/get-link-with-allowed-hostnames";import { recordClick } from "@/lib/tinybird";import { ratelimit, redis } from "@/lib/upstash";-import { isValidUrl, LOCALHOST_IP, nanoid, punyEncode } from "@dub/utils";+import { isValidUrl, LOCALHOST_IP, nanoid } from "@dub/utils";import { ipAddress, waitUntil } from "@vercel/functions";import { AxiomRequest, withAxiom } from "next-axiom";import { NextResponse } from "next/server";@@ -43,19 +43,7 @@ export const POST = withAxiom(});}- // might wanna cache this- 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;+ const link = await getLinkWithAllowedHostnames(domain, key);if (!link) {throw new DubApiError({diff --git a/apps/web/app/api/track/visit/route.ts b/apps/web/app/api/track/visit/route.tsindex 4e04341f11..bba1a526a6 100644--- a/apps/web/app/api/track/visit/route.ts+++ b/apps/web/app/api/track/visit/route.ts@@ -3,7 +3,7 @@ import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";import { linkCache } from "@/lib/api/links/cache";import { includeTags } from "@/lib/api/links/include-tags";import { createId, parseRequestBody } from "@/lib/api/utils";-import { conn } from "@/lib/planetscale/connection";+import { getLinkWithAllowedHostnames } from "@/lib/planetscale/get-link-with-allowed-hostnames";import { recordClick, recordLink } from "@/lib/tinybird";import { ratelimit, redis } from "@/lib/upstash";import { prismaEdge } from "@dub/prisma/edge";@@ -12,7 +12,6 @@ import {linkConstructorSimple,LOCALHOST_IP,nanoid,- punyEncode,} from "@dub/utils";import { ipAddress, waitUntil } from "@vercel/functions";import { AxiomRequest, withAxiom } from "next-axiom";@@ -59,21 +58,22 @@ export const POST = withAxiom(});}- 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(linkKey))],- );-- let link =- rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;+ let link = await getLinkWithAllowedHostnames(domain, linkKey);if (!link) {- // create new link if not exist+ // get root domain link+ const rootDomainLink = await getLinkWithAllowedHostnames(+ domain,+ "_root",+ );++ if (!rootDomainLink) {+ throw new DubApiError({+ code: "not_found",+ message: "siteDomain not found – have you created it on Dub yet?",+ });+ }+const newLink = await prismaEdge.link.create({data: {id: createId("link_"),@@ -82,27 +82,19 @@ export const POST = withAxiom(url,shortLink: linkConstructorSimple({ domain, key: linkKey }),trackConversion: true,- folderId: "fold_fjA8lslBy2qFcosUrwWFxmfk",- },- include: {- ...includeTags,- project: {- select: {- allowedHostnames: true,- },- },+ projectId: rootDomainLink.projectId,+ folderId: rootDomainLink.folderId,+ userId: rootDomainLink.userId,},+ include: includeTags,});waitUntil(Promise.allSettled([linkCache.set(newLink), recordLink(newLink)]),);link = {- id: newLink.id,- url: newLink.url,- projectId: newLink.projectId!,- allowedHostnames:- (newLink.project?.allowedHostnames as string[]) || [],+ ...newLink,+ ...rootDomainLink,};}diff --git a/apps/web/lib/planetscale/get-link-with-allowed-hostnames.ts b/apps/web/lib/planetscale/get-link-with-allowed-hostnames.tsnew file mode 100644index 0000000000..4c2e8b373c--- /dev/null+++ b/apps/web/lib/planetscale/get-link-with-allowed-hostnames.ts@@ -0,0 +1,21 @@+import { punyEncode } from "@dub/utils";+import { conn } from "./connection";++export const getLinkWithAllowedHostnames = async (+ domain: string,+ key: string,+) => {+ const { rows } = await conn.execute<{+ id: string;+ url: string;+ projectId: string;+ folderId: string | null;+ userId: string;+ allowedHostnames: string[];+ }>(+ "SELECT Link.id, Link.url, projectId, folderId, userId, allowedHostnames FROM Link LEFT JOIN Project ON Link.projectId = Project.id WHERE domain = ? AND `key` = ?",+ [domain, punyEncode(decodeURIComponent(key))],+ );++ return rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;+};