The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Improved domains cron
Steven Tey885dca5249ff
diff --git a/app/ui/home/pricing.tsx b/app/ui/home/pricing.tsxindex 380d0d9d5e..f4b0391e0c 100644--- a/app/ui/home/pricing.tsx+++ b/app/ui/home/pricing.tsx@@ -130,7 +130,7 @@ const Pricing = ({ homePage }: { homePage?: boolean }) => {<span className="bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-transparent">affordable</span>{" "}- pricing.+ pricing</h2><p className="mt-5 text-gray-600 sm:text-lg">Shorten your links without breaking your bank. <br />diff --git a/components/app/modals/upgrade-plan-modal/pricing.tsx b/components/app/modals/upgrade-plan-modal/pricing.tsxindex 7af3162b64..c5fbed8b0d 100644--- a/components/app/modals/upgrade-plan-modal/pricing.tsx+++ b/components/app/modals/upgrade-plan-modal/pricing.tsx@@ -137,7 +137,7 @@ const Pricing = ({ homePage }: { homePage?: boolean }) => {<span className="bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-transparent">affordable</span>{" "}- pricing.+ pricing</h2><p className="mt-5 text-gray-600 sm:text-lg">Shorten your links without breaking your bank. <br />diff --git a/lib/api/domains.ts b/lib/api/domains.tsindex 982ec4fcd4..5082234b5e 100644--- a/lib/api/domains.ts+++ b/lib/api/domains.ts@@ -228,8 +228,8 @@ export async function changeDomainForImages(domain: string, newDomain: string) {}}-/* Delete all links & images associated with a domain when it's deleted */-export async function deleteDomainLinks(domain: string) {+/* Delete a domain and all links & images associated with with */+export async function deleteDomainAndLinks(domain: string) {const links = await prisma.link.findMany({where: {domain,@@ -244,5 +244,13 @@ export async function deleteDomainLinks(domain: string) {pipeline.exec(), // delete all links from redis// remove all images from cloudinarycloudinary.v2.api.delete_resources_by_prefix(domain),+ // remove the domain from Vercel+ removeDomainFromVercel(domain),+ // remove the domain from the database+ prisma.domain.delete({+ where: {+ slug: domain,+ },+ }),]);}diff --git a/lib/cron/domains.tsx b/lib/cron/domains.tsxindex 568df64b28..2921d8fbcf 100644--- a/lib/cron/domains.tsx+++ b/lib/cron/domains.tsx@@ -2,9 +2,9 @@ import sendMail from "emails";import InvalidDomain from "emails/InvalidDomain";import DomainDeleted from "emails/DomainDeleted";import { log } from "@/lib/utils";-import { removeDomainFromVercel } from "@/lib/api/domains";+import { deleteDomainAndLinks } from "@/lib/api/domains";import prisma from "@/lib/prisma";-import { redis } from "../upstash";+import { getClicksForDomain } from "../tinybird";export const handleDomainUpdates = async ({domain,@@ -59,6 +59,11 @@ export const handleDomainUpdates = async ({},},},+ _count: {+ select: {+ domains: true,+ },+ },},});if (!project) {@@ -76,35 +81,46 @@ export const handleDomainUpdates = async ({// if domain is invalid for more than 30 days, check if we can delete itif (invalidDays >= 30) {// only delete if there are no links associated with the domain- if (linksCount === 0) {- return await Promise.all([- removeDomainFromVercel(domain), // remove domain from Vercel project- redis.del(`root:${domain}`), // there are no links anyway, so you can just delete the root domain from redis if exists- prisma.domain.delete({- where: {- slug: domain,- },- }),- log(- `Domain *${domain}* has been invalid for > 30 days and has no links, deleting.`,+ if (linksCount > 0) {+ // if there are links, check if there are any link clicks+ const clicksForDomain = await getClicksForDomain(domain);+ if (clicksForDomain > 0) {+ await log(+ `Domain *${domain}* has been invalid for > 30 days but has link clicks, not deleting.`,"cron",- ),- sendMail({- subject: `Your domain ${domain} has been deleted`,- to: ownerEmail,- component: (- <DomainDeleted domain={domain} projectSlug={projectSlug} />- ),- }),- ]);- } else {- await log(- `Domain *${domain}* has been invalid for > 30 days but has links, not deleting.`,- "cron",- true,- );+ true,+ );+ return;+ }}- return;+ // check if the project needs to be deleted as well+ const deleteProjectAsWell = project._count.domains === 1;++ return await Promise.allSettled([+ deleteDomainAndLinks(domain).then(() => {+ deleteProjectAsWell &&+ prisma.project.delete({+ where: {+ slug: projectSlug,+ },+ });+ }),+ log(+ `Domain *${domain}* has been invalid for > 30 days and ${+ linksCount > 0 ? "has links but no link clicks" : "has no links"+ }, deleting. ${+ deleteProjectAsWell+ ? "Since this is the only domain for the project, the project will be deleted as well."+ : ""+ }`,+ "cron",+ ),+ sendMail({+ subject: `Your domain ${domain} has been deleted`,+ to: ownerEmail,+ component: <DomainDeleted domain={domain} projectSlug={projectSlug} />,+ }),+ ]);}if (invalidDays >= 28) {@@ -154,7 +170,7 @@ const sendDomainInvalidEmail = async ({ownerEmail: string;type: "first" | "second";}) => {- return await Promise.all([+ return await Promise.allSettled([log(`Domain *${domain}* is invalid for ${invalidDays} days, email sent.`,"cron",diff --git a/lib/tinybird.ts b/lib/tinybird.tsindex 07cc5af03c..399b7254d6 100644--- a/lib/tinybird.ts+++ b/lib/tinybird.ts@@ -68,19 +68,9 @@ export async function recordClick(]);}-export async function getClicksUsage({- domain,- start,- end,-}: {- domain: string;- start?: string;- end?: string;-}) {+export async function getClicksForDomain(domain: string) {const response = await fetch(- `https://api.us-east.tinybird.co/v0/pipes/usage.json?domain=${domain}${- start ? `&start=${start}` : ""- }${end ? `&end=${end}` : ""}`,+ `https://api.us-east.tinybird.co/v0/pipes/usage.json?domain=${domain}`,{headers: {Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,diff --git a/pages/api/cron/domains.ts b/pages/api/cron/domains.tsindex 3ce530929c..67b3a84e9c 100644--- a/pages/api/cron/domains.ts+++ b/pages/api/cron/domains.ts@@ -117,8 +117,7 @@ async function handler(_req: NextApiRequest, res: NextApiResponse) {/*** verifySignature will try to load `QSTASH_CURRENT_SIGNING_KEY` and `QSTASH_NEXT_SIGNING_KEY` from the environment.- * To test out the endpoint manually (wihtout using QStash), you can do `export default handler` instead and- * hit this endpoint via http://localhost:3000/api/cron/domains+ * This will only run in production. In development, it will return the handler without verifying the signature.*/const Cron = () => {if (process.env.NODE_ENV === "development") {diff --git a/pages/api/cron/usage.ts b/pages/api/cron/usage.tsindex eb0822ecc1..61202a5abc 100644--- a/pages/api/cron/usage.ts+++ b/pages/api/cron/usage.ts@@ -21,8 +21,7 @@ async function handler(_req: NextApiRequest, res: NextApiResponse) {/*** verifySignature will try to load `QSTASH_CURRENT_SIGNING_KEY` and `QSTASH_NEXT_SIGNING_KEY` from the environment.- * To test out the endpoint manually (wihtout using QStash), you can do `export default handler` instead and- * hit this endpoint via http://localhost:3000/api/cron/domains+ * This will only run in production. In development, it will return the handler without verifying the signature.*/const Cron = () => {if (process.env.NODE_ENV === "development") {diff --git a/pages/api/projects/[slug]/domains/[domain]/index.ts b/pages/api/projects/[slug]/domains/[domain]/index.tsindex 4136ff09ae..a7eb15aaec 100644--- a/pages/api/projects/[slug]/domains/[domain]/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/index.ts@@ -8,7 +8,7 @@ import prisma from "@/lib/prisma";import {changeDomainForImages,changeDomainForLinks,- deleteDomainLinks,+ deleteDomainAndLinks,setRootDomain,} from "@/lib/api/domains";@@ -108,15 +108,7 @@ export default withProjectAuth(async (req, res, project) => {// DELETE /api/projects/[slug]/domains/[domain] delete a project's domain} else if (req.method === "DELETE") {- const response = await Promise.allSettled([- removeDomainFromVercel(domain),- deleteDomainLinks(domain),- prisma.domain.delete({- where: {- slug: domain,- },- }),- ]);+ const response = await deleteDomainAndLinks(domain);return res.status(200).json(response);} else {res.setHeader("Allow", ["PUT", "DELETE"]);diff --git a/pages/api/projects/[slug]/index.ts b/pages/api/projects/[slug]/index.tsindex e3b95aa55b..8d9dd13f75 100644--- a/pages/api/projects/[slug]/index.ts+++ b/pages/api/projects/[slug]/index.ts@@ -1,5 +1,5 @@import { withProjectAuth } from "@/lib/auth";-import { removeDomainFromVercel, deleteDomainLinks } from "@/lib/api/domains";+import { deleteDomainAndLinks } from "@/lib/api/domains";import prisma from "@/lib/prisma";export default withProjectAuth(async (req, res, project) => {@@ -39,17 +39,20 @@ export default withProjectAuth(async (req, res, project) => {})).map((domain) => domain.slug);- const response = await Promise.allSettled([- prisma.project.delete({- where: {- slug: project.slug,- },- }),- ...domains.map((domain) => removeDomainFromVercel(domain)),- ...domains.map((domain) => deleteDomainLinks(domain)),- ]);+ const deleteDomainsResponse = await Promise.allSettled(+ domains.map((domain) => deleteDomainAndLinks(domain)),+ );++ const deleteProjectResponse = await prisma.project.delete({+ where: {+ slug: project.slug,+ },+ });- return res.status(200).json(response);+ return res.status(200).json({+ deleteProjectResponse,+ deleteDomainsResponse,+ });} else {res.setHeader("Allow", ["GET", "PUT", "DELETE"]);return res.status(405).json({ error: `Method ${req.method} Not Allowed` });