The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
added change domain function
Steven Teyfa54571a3994
diff --git a/components/app/custom-domain/edit-domain-modal.tsx b/components/app/custom-domain/edit-domain-modal.tsxindex 593d4e3b7e..04ace29c69 100644--- a/components/app/custom-domain/edit-domain-modal.tsx+++ b/components/app/custom-domain/edit-domain-modal.tsx@@ -139,6 +139,27 @@ function EditDomainModalHelper({)}</div>+ <div>+ <label+ htmlFor="verification"+ className="block text-sm text-gray-700"+ >+ To verify, type{" "}+ <span className="font-semibold">confirm domain change</span> below+ </label>+ <div className="relative mt-1 rounded-md shadow-sm">+ <input+ type="text"+ name="verification"+ id="verification"+ pattern="confirm domain change"+ required+ autoFocus={false}+ className="border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500 pr-10 block w-full rounded-md focus:outline-none sm:text-sm"+ />+ </div>+ </div>+<buttondisabled={saving || domainExistsError}className={`${diff --git a/lib/middleware/root.ts b/lib/middleware/root.tsindex 61e5c3d416..de324ad30f 100644--- a/lib/middleware/root.ts+++ b/lib/middleware/root.ts@@ -9,7 +9,9 @@ export default function RootMiddleware(req: NextRequest, ev: NextFetchEvent) {return NextResponse.next();}- ev.waitUntil(recordClick(hostname, req)); // record clicks on root page+ if (hostname !== "preview.dub.sh" && !hostname.endsWith(".vercel.app")) {+ ev.waitUntil(recordClick(hostname, req)); // record clicks on root page (if it's not a Vercel preview link)+ }if (hostname === "dub.sh" ||diff --git a/lib/upstash.ts b/lib/upstash.tsindex cc8a6c9d16..8eb62d6a2b 100644--- a/lib/upstash.ts+++ b/lib/upstash.ts@@ -144,16 +144,13 @@ export async function addLink(: await setRandomKey(hostname, url, title);if (response === 1) {- const pipeline = redis.pipeline();- pipeline.zadd(`${hostname}:links:timestamps${userId ? `:${userId}` : ""}`, {- score: Date.now(),- member: key,- });- pipeline.zadd(`${hostname}:links:clicks`, {- score: 0,- member: key,- });- return await pipeline.exec();+ return await redis.zadd(+ `${hostname}:links:timestamps${userId ? `:${userId}` : ""}`,+ {+ score: Date.now(),+ member: key,+ }+ );} else {return null; // key already exists}@@ -199,12 +196,6 @@ export async function editLink(score: timestamp,member: newKey,});- // remove old key from links:clicks and add new key (with same score)- pipeline.zrem(`${hostname}:links:clicks`, key);- pipeline.zadd(`${hostname}:links:clicks`, {- score: numClicks,- member: newKey,- });// update name for clicks:[key] (if numClicks > 0, because we don't create clicks:[key] until the first click)if (numClicks > 0) {pipeline.rename(@@ -244,3 +235,25 @@ export async function getUsage(hostname: string) {await redis.setex(`usage:${hostname}`, 3600, usage); // cache for 1 hourreturn usage;}++export async function changeDomain(hostname: string, newHostname: string) {+ const keys = await redis.zrange<string[]>(+ `${hostname}:links:timestamps`,+ 0,+ -1+ );+ const pipeline = redis.pipeline();+ pipeline.rename(`${hostname}:links`, `${newHostname}:links`);+ pipeline.rename(+ `${hostname}:links:timestamps`,+ `${newHostname}:links:timestamps`+ );+ pipeline.rename(`${hostname}:root:clicks`, `${newHostname}:root:clicks`);+ keys.forEach((key) => {+ pipeline.rename(+ `${hostname}:clicks:${key}`,+ `${newHostname}:clicks:${key}`+ );+ });+ return await pipeline.exec();+}diff --git a/pages/api/edge/links/index.ts b/pages/api/edge/links/index.tsindex ff3d9ea856..f6e1cf433c 100644--- a/pages/api/edge/links/index.ts+++ b/pages/api/edge/links/index.ts@@ -14,16 +14,10 @@ export default async function handler(req: NextRequest) {const { response, key } = await setRandomKey("dub.sh", url);if (response === 1) {// if key was successfully added- const pipeline = redis.pipeline();- pipeline.zadd(`dub.sh:links:timestamps:generic`, {+ await redis.zadd(`dub.sh:links:timestamps:generic`, {score: Date.now(),member: key,});- pipeline.zadd(`dub.sh:links:clicks`, {- score: 0,- member: key,- });- await pipeline.exec();return new Response(JSON.stringify({key,diff --git a/pages/api/projects/[slug]/domains/[domain]/index.ts b/pages/api/projects/[slug]/domains/[domain]/index.tsindex 95387fd97d..5edb9113bf 100644--- a/pages/api/projects/[slug]/domains/[domain]/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/index.ts@@ -1,6 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import prisma from "@/lib/prisma";import { withProjectAuth } from "@/lib/auth";+import { changeDomain } from "@/lib/upstash";export default withProjectAuth(async (req: NextApiRequest, res: NextApiResponse) => {@@ -42,6 +43,8 @@ export default withProjectAuth(),]);+ const upstashResponse = await changeDomain(domain, newDomain);+const response = await prisma.project.update({where: {slug,