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.

improve dotLinkClaimed + configureDNS

Steven Tey1548cfea3fb8
diff --git a/apps/web/app/api/cron/domains/configure-dns/route.ts b/apps/web/app/api/cron/domains/configure-dns/route.ts
new file mode 100644
index 0000000000..613a31755c
--- /dev/null
+++ b/apps/web/app/api/cron/domains/configure-dns/route.ts
@@ -0,0 +1,23 @@
+import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { configureDNS } from "@/lib/dynadot/configure-dns";
+export const dynamic = "force-dynamic";
+
+/*
+ This route is used to configure the DNS for a domain.
+ It is called by QStash 3 minutes after a domain is claimed.
+*/
+export async function POST(req: Request) {
+ try {
+ const body = await req.json();
+ await verifyQstashSignature(req, body);
+ const { domain } = body;
+
+ const res = await configureDNS({ domain });
+ console.log("Dynadot DNS configured.", res);
+
+ return new Response("Dynadot DNS configured.", { status: 200 });
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/domains/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/domains/page-client.tsx
index df8558f224..1250282504 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/domains/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/domains/page-client.tsx
@@ -1,6 +1,5 @@
"use client";
-import { checkDotlinkClaimed } from "@/lib/actions/check-dotlink-claimed";
import { clientAccessCheck } from "@/lib/api/tokens/permissions";
import useDomains from "@/lib/swr/use-domains";
import useDomainsCount from "@/lib/swr/use-domains-count";
@@ -27,7 +26,6 @@ import { ToggleGroup } from "@dub/ui/src/toggle-group";
import { InfoTooltip, TooltipContent } from "@dub/ui/src/tooltip";
import { capitalize } from "@dub/utils";
import { ChevronDown, Crown } from "lucide-react";
-import { useAction } from "next-safe-action/hooks";
import { useEffect, useState } from "react";
import { DefaultDomains } from "./default-domains";
@@ -40,6 +38,7 @@ export default function WorkspaceDomainsClient() {
domainsLimit,
exceededDomains,
flags,
+ dotLinkClaimed,
} = useWorkspace();
const [openPopover, setOpenPopover] = useState(false);
@@ -47,19 +46,6 @@ export default function WorkspaceDomainsClient() {
const { allWorkspaceDomains, loading } = useDomains({ includeParams: true });
const { data: domainsCount } = useDomainsCount();
- const [dotLinkClaimed, setDotLinkClaimed] = useState(false);
- const { executeAsync, isExecuting } = useAction(checkDotlinkClaimed);
-
- useEffect(() => {
- const checkDotLink = async () => {
- if (workspaceId && flags?.dotlink) {
- const result = await executeAsync({ workspaceId });
- setDotLinkClaimed(Boolean(result?.data));
- }
- };
- checkDotLink();
- }, [workspaceId, flags?.dotlink]);
-
const { pagination, setPagination } = usePagination(50);
const archived = searchParams.get("archived");
@@ -186,7 +172,7 @@ export default function WorkspaceDomainsClient() {
icon={<LinkBroken className="size-4" />}
className="h-9 justify-start px-2 text-gray-800 disabled:border-none disabled:bg-transparent disabled:text-gray-500"
onClick={() => setShowRegisterDomainModal(true)}
- disabled={isExecuting || dotLinkClaimed}
+ disabled={dotLinkClaimed}
/>
</div>
}
diff --git a/apps/web/lib/actions/check-dotlink-claimed.ts b/apps/web/lib/actions/check-dotlink-claimed.ts
deleted file mode 100644
index cc0a2de241..0000000000
--- a/apps/web/lib/actions/check-dotlink-claimed.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-"use server";
-
-import { getRegisteredDotlinkDomain } from "../api/domains/get-registered-dotlink-domain";
-import z from "../zod";
-import { authActionClient } from "./safe-action";
-
-const schema = z.object({
- workspaceId: z.string(),
-});
-
-// check if a workspace has claimed their free .link domain
-export const checkDotlinkClaimed = authActionClient
- .schema(schema)
- .action(async ({ ctx }) => {
- const { workspace } = ctx;
-
- const registeredDotLinkDomain = await getRegisteredDotlinkDomain(
- workspace.id,
- );
-
- return registeredDotLinkDomain ? true : false;
- });
diff --git a/apps/web/lib/api/domains/claim-dot-link-domain.ts b/apps/web/lib/api/domains/claim-dot-link-domain.ts
index 74ced032d1..aeed60b62b 100644
--- a/apps/web/lib/api/domains/claim-dot-link-domain.ts
+++ b/apps/web/lib/api/domains/claim-dot-link-domain.ts
@@ -1,10 +1,14 @@
import { DubApiError } from "@/lib/api/errors";
import { createLink } from "@/lib/api/links";
-import { configureDNS } from "@/lib/dynadot/configure-dns";
+import { qstash } from "@/lib/cron";
import { registerDomain } from "@/lib/dynadot/register-domain";
import { prisma } from "@/lib/prisma";
import { WorkspaceWithUsers } from "@/lib/types";
-import { DEFAULT_LINK_PROPS } from "@dub/utils";
+import {
+ ACME_WORKSPACE_ID,
+ APP_DOMAIN_WITH_NGROK,
+ DEFAULT_LINK_PROPS,
+} from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { sendEmail } from "emails";
import DomainClaimed from "emails/domain-claimed";
@@ -27,15 +31,17 @@ export async function claimDotLinkDomain({
message: "Free workspaces cannot register .link domains.",
});
- const registeredDotLinkDomain = await getRegisteredDotlinkDomain(
- workspace.id,
- );
+ if (workspace.id !== ACME_WORKSPACE_ID) {
+ const registeredDotLinkDomain = await getRegisteredDotlinkDomain(
+ workspace.id,
+ );
- if (registeredDotLinkDomain) {
- throw new DubApiError({
- code: "forbidden",
- message: "Workspace is limited to one free .link domain.",
- });
+ if (registeredDotLinkDomain) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "Workspace is limited to one free .link domain.",
+ });
+ }
}
const [response, totalDomains, matchingUnverifiedDomain] = await Promise.all([
@@ -102,46 +108,68 @@ export async function claimDotLinkDomain({
]);
waitUntil(
- (async () => {
- await Promise.all([
- // configure the DNS with Dynadot
- configureDNS({ domain }),
- // add domain to Vercel
- addDomainToVercel(domain),
- ]);
-
- // once domain is provisioned, send email
- const workspaceWithOwner = await prisma.project.findUniqueOrThrow({
+ Promise.all([
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/configure-dns`,
+ // delete after 3 mins
+ delay: 3 * 60,
+ body: {
+ domain,
+ },
+ }),
+ // add domain to Vercel
+ addDomainToVercel(domain),
+ // send email to workspace owners
+ sendDomainClaimedEmails({ workspace, domain }),
+ // update workspace to set dotLinkClaimed to true
+ prisma.project.update({
where: {
id: workspace.id,
},
- include: {
- users: {
- where: {
- role: "owner",
- },
- select: {
- user: true,
- },
- },
+ data: {
+ dotLinkClaimed: true,
},
- });
-
- workspaceWithOwner.users.map(({ user }) => {
- if (user.email) {
- sendEmail({
- email: user.email,
- subject: "Successfully claimed your .link domain!",
- react: DomainClaimed({
- email: user.email,
- domain,
- workspaceSlug: workspace.slug,
- }),
- });
- }
- });
- })(),
+ }),
+ ]),
);
return response;
}
+
+const sendDomainClaimedEmails = async ({
+ workspace,
+ domain,
+}: {
+ workspace: Pick<WorkspaceWithUsers, "id" | "slug">;
+ domain: string;
+}) => {
+ const workspaceWithOwner = await prisma.project.findUniqueOrThrow({
+ where: {
+ id: workspace.id,
+ },
+ include: {
+ users: {
+ where: {
+ role: "owner",
+ },
+ select: {
+ user: true,
+ },
+ },
+ },
+ });
+
+ workspaceWithOwner.users.map(({ user }) => {
+ if (user.email) {
+ sendEmail({
+ email: user.email,
+ subject: "Successfully claimed your .link domain!",
+ react: DomainClaimed({
+ email: user.email,
+ domain,
+ workspaceSlug: workspace.slug,
+ }),
+ });
+ }
+ });
+};
diff --git a/apps/web/lib/dynadot/configure-dns.ts b/apps/web/lib/dynadot/configure-dns.ts
index 2e3d27c518..e6f48b7645 100644
--- a/apps/web/lib/dynadot/configure-dns.ts
+++ b/apps/web/lib/dynadot/configure-dns.ts
@@ -13,5 +13,5 @@ export const configureDNS = async ({ domain }: { domain: string }) => {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
- });
+ }).then((res) => res.json());
};
diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.ts
index 4df94ccdb2..d1571973b4 100644
--- a/apps/web/lib/zod/schemas/workspaces.ts
+++ b/apps/web/lib/zod/schemas/workspaces.ts
@@ -73,6 +73,12 @@ export const WorkspaceSchema = z
.describe(
"Whether the workspace has conversion tracking enabled (d.to/conversions).",
),
+ dotLinkClaimed: z
+ .boolean()
+ .describe(
+ "Whether the workspace has claimed a free .link domain. (dub.link/free)",
+ ),
+
createdAt: z
.date()
.describe("The date and time when the workspace was created."),
diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prisma
index 2f873a0681..3365278031 100644
--- a/apps/web/prisma/schema/workspace.prisma
+++ b/apps/web/prisma/schema/workspace.prisma
@@ -29,6 +29,7 @@ model Project {
webhookEnabled Boolean @default(false)
conversionEnabled Boolean @default(false)
ssoEnabled Boolean @default(false)
+ dotLinkClaimed Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
diff --git a/apps/web/ui/domains/register-domain-form.tsx b/apps/web/ui/domains/register-domain-form.tsx
index 73eecfb75c..1058690939 100644
--- a/apps/web/ui/domains/register-domain-form.tsx
+++ b/apps/web/ui/domains/register-domain-form.tsx
@@ -106,8 +106,9 @@ export function RegisterDomainForm({
} else {
toast.success("Domain registered successfully!");
- // Mutate domains and links
+ // Mutate workspace, domains links
await Promise.all([
+ mutate(`/api/workspaces/${workspace.slug}`),
mutate(
(key) =>
typeof key === "string" &&
diff --git a/packages/utils/src/constants/main.ts b/packages/utils/src/constants/main.ts
index a598c86dbf..ecd45977f6 100644
--- a/packages/utils/src/constants/main.ts
+++ b/packages/utils/src/constants/main.ts
@@ -50,6 +50,7 @@ export const DUB_WORDMARK = "https://assets.dub.co/wordmark.png";
export const DUB_THUMBNAIL = "https://assets.dub.co/thumbnail.jpg";
export const DUB_WORKSPACE_ID = "cl7pj5kq4006835rbjlt2ofka";
+export const ACME_WORKSPACE_ID = "clrei1gld0002vs9mzn93p8ik";
export const LEGAL_WORKSPACE_ID = "clrflia0j0000vs7sqfhz9c7q";
export const LEGAL_USER_ID = "clqei1lgc0000vsnzi01pbf47";