The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix everything
Steven Teya4b2e337c65d
diff --git a/apps/web/app/api/admin/links/[linkId]/route.ts b/apps/web/app/api/admin/links/[linkId]/route.tsindex ecf98842af..0c4a1d80e4 100644--- a/apps/web/app/api/admin/links/[linkId]/route.ts+++ b/apps/web/app/api/admin/links/[linkId]/route.ts@@ -1,32 +1,19 @@-import { getDomain } from "@/lib/api/domains/get-domain";import { withAdmin } from "@/lib/auth";import { prisma } from "@/lib/prisma";-import { LinkProps } from "@/lib/types";import { NextResponse } from "next/server";// GET /api/admin/links/[linkId] – get a link as an adminexport const GET = withAdmin(async ({ params }) => {const { linkId } = params;- let link = await prisma.link.findUnique({+ const link = await prisma.link.findUnique({where: {id: linkId,},});if (!link) {- const domain = await getDomain({- id: linkId,- });-- if (domain) {- link = {- ...domain,- domain: domain.slug,- key: "_root",- url: domain.target || "",- } as unknown as LinkProps;- }+ return NextResponse.json({ error: "Link not found" }, { status: 404 });}return NextResponse.json(link);diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.tsindex 88d75feff3..deedc2d140 100644--- a/apps/web/app/api/domains/[domain]/route.ts+++ b/apps/web/app/api/domains/[domain]/route.ts@@ -91,7 +91,7 @@ export const PATCH = withWorkspace(// remove old domain from VercelremoveDomainFromVercel(domain),// rename redis key- redis.rename(domain, newDomain),+ redis.rename(domain.toLowerCase(), newDomain.toLowerCase()),]);const allLinks = await prisma.link.findMany({@@ -104,8 +104,8 @@ export const PATCH = withWorkspace(});// update all links in Tinybird- recordLink([- ...allLinks.map((link) => ({+ recordLink(+ allLinks.map((link) => ({link_id: link.id,domain: link.domain,key: link.key,@@ -114,7 +114,7 @@ export const PATCH = withWorkspace(workspace_id: link.projectId,created_at: link.createdAt,})),- ]);+ );}})(),);diff --git a/apps/web/app/api/domains/[domain]/transfer/route.ts b/apps/web/app/api/domains/[domain]/transfer/route.tsindex 8b990f006a..06f1e7573b 100644--- a/apps/web/app/api/domains/[domain]/transfer/route.ts+++ b/apps/web/app/api/domains/[domain]/transfer/route.ts@@ -1,5 +1,4 @@import { getAnalytics } from "@/lib/analytics/get-analytics";-import { getDomain } from "@/lib/api/domains/get-domain";import { DubApiError } from "@/lib/api/errors";import { withWorkspace } from "@/lib/auth";import { qstash } from "@/lib/cron";@@ -55,11 +54,6 @@ export const POST = withWorkspace(});}- const domainRecord = await getDomain({- slug: domain,- workspaceId: workspace.id,- });-if (newWorkspace.domains.length >= newWorkspace.domainsLimit) {throw new DubApiError({code: "exceeded_limit",@@ -91,7 +85,6 @@ export const POST = withWorkspace(groupBy: "count",workspaceId: workspace.id,interval: "30d",- root: false,});// Update the domain to use the new workspacediff --git a/apps/web/lib/api/domains/get-domain.ts b/apps/web/lib/api/domains/get-domain.tsdeleted file mode 100644index c4bf0b90a5..0000000000--- a/apps/web/lib/api/domains/get-domain.ts+++ /dev/null@@ -1,41 +0,0 @@-import { prisma } from "@/lib/prisma";-import { DubApiError } from "../errors";--type GetDomainInput =- | {- slug: string;- workspaceId: string;- }- | {- id: string;- };--export const getDomain = async (input: GetDomainInput) => {- const domain = await prisma.domain.findUnique({- where: {- ...("id" in input- ? { id: input.id }- : { slug: input.slug, projectId: input.workspaceId }),- },- include: {- links: {- select: {- url: true,- rewrite: true,- clicks: true,- expiredUrl: true,- noindex: true,- },- },- },- });-- if (!domain) {- throw new DubApiError({- code: "not_found",- message: `Domain not found in the workspace.`,- });- }-- return domain;-};diff --git a/apps/web/lib/api/domains/index.ts b/apps/web/lib/api/domains/index.tsindex bb9df5d545..416d21d6e6 100644--- a/apps/web/lib/api/domains/index.ts+++ b/apps/web/lib/api/domains/index.ts@@ -3,6 +3,5 @@ export * from "./delete-domain-links";export * from "./get-config-response";export * from "./get-domain-response";export * from "./remove-domain-vercel";-export * from "./set-root-domain";export * from "./utils";export * from "./verify-domain";diff --git a/apps/web/lib/api/domains/set-root-domain.ts b/apps/web/lib/api/domains/set-root-domain.tsdeleted file mode 100644index 4ff6f7139d..0000000000--- a/apps/web/lib/api/domains/set-root-domain.ts+++ /dev/null@@ -1,58 +0,0 @@-import { redis } from "@/lib/upstash";-import { isIframeable } from "@dub/utils";--export async function setRootDomain({- id,- domain,- domainCreatedAt,- projectId,- url,- rewrite,- noindex,- newDomain,-}: {- id: string;- domain: string;- domainCreatedAt: Date;- projectId: string;- url?: string;- rewrite?: boolean;- noindex?: boolean;- newDomain?: string; // if the domain is changed, this will be the new domain-}) {- if (newDomain) {- await redis.rename(domain.toLowerCase(), newDomain.toLowerCase());- }- return await Promise.allSettled([- redis.hset(newDomain ? newDomain.toLowerCase() : domain.toLowerCase(), {- _root: {- id,- ...(url && {- url,- }),- ...(url &&- rewrite && {- rewrite: true,- iframeable: await isIframeable({- url,- requestDomain: newDomain- ? newDomain.toLowerCase()- : domain.toLowerCase(),- }),- }),- ...(noindex && {- noindex: true,- }),- projectId,- },- }),- // recordLink({- // link_id: id,- // domain: newDomain || domain,- // key: "_root",- // url: url || "",- // workspace_id: projectId,- // created_at: domainCreatedAt,- // }),- ]);-}diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.tsindex b8637afdce..5588b7e5cc 100644--- a/apps/web/lib/api/links/process-link.ts+++ b/apps/web/lib/api/links/process-link.ts@@ -59,7 +59,7 @@ export async function processLink<T extends Record<string, any>>({ios,android,geo,- noindex,+ doIndex,tagNames,createdAt,} = payload;@@ -108,7 +108,7 @@ export async function processLink<T extends Record<string, any>>({ios ||android ||geo ||- (noindex && key === "_root")+ doIndex) {const proFeaturesString = [proxy && "custom social media cards",@@ -118,7 +118,7 @@ export async function processLink<T extends Record<string, any>>({ios && "iOS targeting",android && "Android targeting",geo && "geo targeting",- noindex && "search engine indexing",+ doIndex && "search engine indexing",].filter(Boolean).join(", ")diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.tsindex 82efda1ba2..f62b621b0a 100644--- a/apps/web/lib/auth/workspace.ts+++ b/apps/web/lib/auth/workspace.ts@@ -14,7 +14,6 @@ import {} from "@dub/utils";import { Link as LinkProps } from "@prisma/client";import { waitUntil } from "@vercel/functions";-import { getDomain } from "../api/domains/get-domain";import { isBetaTester } from "../edge-config";import { hashToken } from "./hash-token";import { Session, getSession } from "./utils";@@ -400,20 +399,6 @@ export const withWorkspace = (// link checks (if linkId or domain and key are provided)if ((linkId || (domain && key && key !== "_root")) && !skipLinkChecks) {- // special case for getting domain by ID- // TODO: refactor domains to use the same logic as links- if (!link && searchParams.checkDomain === "true") {- const domain = await getDomain({ id: linkId! });- if (domain) {- link = {- ...domain,- domain: domain.slug,- key: "_root",- url: domain.target || "",- } as unknown as LinkProps;- }- }-// make sure the link is owned by the workspaceif (!link || link.projectId !== workspace?.id) {throw new DubApiError({diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.tsindex ff9712882f..089adb658e 100644--- a/apps/web/lib/middleware/link.ts+++ b/apps/web/lib/middleware/link.ts@@ -35,10 +35,6 @@ export default async function LinkMiddleware(return NextResponse.next();}- if (!key) {- key = "_root";- }-// encode the key to ascii// links on Dub are case insensitive by defaultkey = punyEncode(key.toLowerCase());@@ -62,6 +58,11 @@ export default async function LinkMiddleware(key = key.slice(0, -1);}+ // if key is empty string, set to _root (root domain link)+ if (key === "") {+ key = "_root";+ }+let link = await redis.hget<RedisLinkProps>(domain, key);if (!link) {@@ -73,7 +74,6 @@ export default async function LinkMiddleware(return NextResponse.redirect(new URL("/", req.url), {headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),},status: 302,});@@ -102,8 +102,13 @@ export default async function LinkMiddleware(android,geo,expiredUrl,+ doIndex,} = link;+ // by default, we only index default dub domain links (e.g. dub.sh)+ // everything else is not indexed by default, unless the user has explicitly set it to be indexed+ const shouldIndex = isDubDomain(domain) || doIndex === true;+// only show inspect modal if the link is not password protectedif (inspectMode && !password) {return NextResponse.rewrite(@@ -111,7 +116,7 @@ export default async function LinkMiddleware({headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},},);@@ -131,7 +136,7 @@ export default async function LinkMiddleware({headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && {+ ...(!shouldIndex && {"X-Robots-Tag": "googlebot: noindex",}),},@@ -148,7 +153,7 @@ export default async function LinkMiddleware(return NextResponse.rewrite(new URL("/banned", req.url), {headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},});}@@ -159,14 +164,14 @@ export default async function LinkMiddleware(return NextResponse.redirect(expiredUrl, {headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},});} else {return NextResponse.rewrite(new URL(`/expired/${domain}`, req.url), {headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},});}@@ -191,12 +196,15 @@ export default async function LinkMiddleware();}+ // for root domain links, if there's no destination URL, rewrite to placeholder pageif (!url) {- // rewrite to placeholder page unless the user defines a site to redirect toreturn NextResponse.rewrite(new URL(`/${domain}`, req.url), {headers: {...DUB_HEADERS,+ // we only index root domain links if they're not subdomains+ ...(shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},+ status: 302,});}@@ -212,7 +220,7 @@ export default async function LinkMiddleware({headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},},);@@ -224,7 +232,7 @@ export default async function LinkMiddleware({headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},},);@@ -237,7 +245,7 @@ export default async function LinkMiddleware({headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && {+ ...(!shouldIndex && {"X-Robots-Tag": "googlebot: noindex",}),},@@ -248,7 +256,7 @@ export default async function LinkMiddleware(return NextResponse.rewrite(url, {headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},});}@@ -263,9 +271,9 @@ export default async function LinkMiddleware({headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},- status: 302,+ status: key === "_root" ? 301 : 302,},);@@ -279,9 +287,9 @@ export default async function LinkMiddleware({headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},- status: 302,+ status: key === "_root" ? 301 : 302,},);@@ -295,9 +303,9 @@ export default async function LinkMiddleware({headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},- status: 302,+ status: key === "_root" ? 301 : 302,},);@@ -311,9 +319,9 @@ export default async function LinkMiddleware({headers: {...DUB_HEADERS,- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},- status: 302,+ status: key === "_root" ? 301 : 302,},);}diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex d59856c4e1..800ac64eff 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -24,7 +24,7 @@ export interface QRLinkProps {export interface RedisLinkProps {id: string;- url: string;+ url?: string;trackConversion?: boolean;password?: boolean;proxy?: boolean;@@ -35,6 +35,7 @@ export interface RedisLinkProps {ios?: string;android?: string;geo?: object;+ doIndex?: boolean;projectId?: string;}@@ -131,15 +132,6 @@ export interface DomainProps {projectId: string;}-export interface RedisDomainProps {- id: string;- url?: string;- rewrite?: boolean;- iframeable?: boolean;- projectId: string;- noindex?: boolean;-}-export interface BitlyGroupProps {guid: string;bsds: string[]; // custom domainsdiff --git a/apps/web/lib/upstash.ts b/apps/web/lib/upstash.tsindex a76cadc6f4..48a9c23337 100644--- a/apps/web/lib/upstash.ts+++ b/apps/web/lib/upstash.ts@@ -1,7 +1,7 @@import { getDomainWithoutWWW, isIframeable } from "@dub/utils";import { Ratelimit } from "@upstash/ratelimit";import { Redis } from "@upstash/redis";-import { LinkProps, RedisDomainProps, RedisLinkProps } from "./types";+import { LinkProps, RedisLinkProps } from "./types";// Initiate Redis instance by connecting to REST URLexport const redis = new Redis({@@ -72,47 +72,28 @@ export async function formatRedisLink(ios,android,geo,+ doIndex,projectId,- noindex,} = link;const hasPassword = password && password.length > 0 ? true : false;return {id,- url,+ ...(url && { url }), // on free plans you cannot set a root domain redirect, hence URL is undefined...(trackConversion && { trackConversion: true }),...(hasPassword && { password: true }),...(proxy && { proxy: true }),- ...(rewrite && {- rewrite: true,- iframeable: await isIframeable({ url, requestDomain: domain }),- }),+ ...(url &&+ rewrite && {+ rewrite: true,+ iframeable: await isIframeable({ url, requestDomain: domain }),+ }),...(expiresAt && { expiresAt: new Date(expiresAt) }),...(expiredUrl && { expiredUrl }),...(ios && { ios }),...(android && { android }),...(geo && { geo: geo as object }),...(projectId && { projectId }), // projectId can be undefined for anonymous links- ...(noindex && { noindex: true }),- };-}--// TODO:-// Can we replace this with the formatRedisLink function?-export async function formatRedisDomain(- link: LinkProps,-): Promise<RedisDomainProps> {- const { id, domain, url, rewrite, projectId, noindex } = link;-- return {- id,- ...(url && { url }), // on free plans you cannot set a root domain redirect, hence URL is undefined- ...(url &&- rewrite && {- rewrite: true,- iframeable: await isIframeable({ url, requestDomain: domain }),- }),- projectId: projectId!,- ...(noindex && { noindex: true }),+ ...(doIndex && { doIndex: true }),};}diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex b5a298534f..43cf809cbf 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -236,12 +236,12 @@ export const createLinkBodySchema = z.object({"Geo targeting information for the short link in JSON format `{[COUNTRY]: https://example.com }`.",).openapi({ ref: "linkGeoTargeting" }),- noindex: z+ doIndex: z.boolean().optional()- .default(true)+ .default(false).describe(- "Prevent search engines from indexing the short link. Defaults to `true` if not provided. Learn more: https://d.to/noindex",+ "Allow search engines to index your short link. Defaults to `false` if not provided. Learn more: https://d.to/noindex",),});@@ -325,10 +325,10 @@ export const LinkSchema = z.boolean().default(false).describe("Whether the short link uses link cloaking."),- noindex: z+ doIndex: z.boolean().default(false)- .describe("Whether to noindex the short link."),+ .describe("Whether to allow search engines to index the short link."),ios: z.string().nullable()diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prismaindex 92f57af45f..18be355c46 100644--- a/apps/web/prisma/schema.prisma+++ b/apps/web/prisma/schema.prisma@@ -237,7 +237,7 @@ model Link {// Link cloaking/masking via rewriterewrite Boolean @default(false) // whether to rewrite the link or not- noindex Boolean @default(true) // don't index short links by default (d.to/noindex)+ doIndex Boolean @default(false) // we don't index short links by default// Custom device targetingios String? @db.LongText // custom link for iOS devicesdiff --git a/apps/web/scripts/bulk-create-domains.ts b/apps/web/scripts/bulk-create-domains.tsindex 85afc29dca..d3ad32ffbf 100644--- a/apps/web/scripts/bulk-create-domains.ts+++ b/apps/web/scripts/bulk-create-domains.ts@@ -55,7 +55,7 @@ async function main() {projectId,archived: false,publicStats: false,- noindex: false,+ doIndex: false,trackConversion: false,proxy: false,rewrite: false,diff --git a/apps/web/scripts/migrate-domains.ts b/apps/web/scripts/migrate-domains.tsindex b01ad26ce1..132657f8aa 100644--- a/apps/web/scripts/migrate-domains.ts+++ b/apps/web/scripts/migrate-domains.ts@@ -42,12 +42,10 @@ async function main() {? {expiredUrl: null,rewrite: false,- noindex: false,}: {expiredUrl: domain.expiredUrl || null,rewrite: domain.type === "rewrite",- noindex: domain.noindex,}),};});diff --git a/apps/web/tests/utils/schema.ts b/apps/web/tests/utils/schema.tsindex 87cd97ea12..7e28205329 100644--- a/apps/web/tests/utils/schema.ts+++ b/apps/web/tests/utils/schema.ts@@ -19,7 +19,7 @@ export const expectedLink: Partial<Link> & { tagId: string | null } = {utm_term: null,utm_content: null,rewrite: false,- noindex: true,+ doIndex: false,ios: null,android: null,geo: null,diff --git a/apps/web/ui/modals/add-edit-domain-modal/index.tsx b/apps/web/ui/modals/add-edit-domain-modal/index.tsxindex 275af3a1f3..927e01775a 100644--- a/apps/web/ui/modals/add-edit-domain-modal/index.tsx+++ b/apps/web/ui/modals/add-edit-domain-modal/index.tsx@@ -142,7 +142,6 @@ function AddEditDomainModal({domain: data.slug,key: "_root",url: "",- noindex: false,}),});}diff --git a/apps/web/ui/modals/add-edit-link-modal/noindex-section.tsx b/apps/web/ui/modals/add-edit-link-modal/doindex-section.tsxsimilarity index 75%rename from apps/web/ui/modals/add-edit-link-modal/noindex-section.tsxrename to apps/web/ui/modals/add-edit-link-modal/doindex-section.tsxindex 8ec7fd7466..fd87ef6bee 100644--- a/apps/web/ui/modals/add-edit-link-modal/noindex-section.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/doindex-section.tsx@@ -3,25 +3,25 @@ import { ProBadgeTooltip } from "@/ui/shared/pro-badge-tooltip";import { SimpleTooltipContent, Switch } from "@dub/ui";import { Dispatch, SetStateAction, useEffect, useState } from "react";-export default function NoindexSection({+export default function DoIndexSection({data,setData,}: {data: LinkProps;setData: Dispatch<SetStateAction<LinkProps>>;}) {- const { noindex } = data;- const [enabled, setEnabled] = useState(!noindex);+ const { doIndex } = data;+ const [enabled, setEnabled] = useState(doIndex);useEffect(() => {if (enabled) {- // if enabling, set noindex to true+ // if enabling, set doIndex to truesetData({...data,- noindex: false,+ doIndex: true,});} else {- // if disabling, set noindex to false- setData({ ...data, noindex: true });+ // if disabling, set doIndex to false+ setData({ ...data, doIndex: false });}}, [enabled]);@@ -35,7 +35,7 @@ export default function NoindexSection({<ProBadgeTooltipcontent={<SimpleTooltipContent- title="Prevent search engines from indexing your short link."+ title="Allow search engines to index your short link. Disabled by default."cta="Learn more."href="https://dub.co/help/article/how-noindex-works"/>diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsxindex d87ec1857d..f8d92ae16d 100644--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx@@ -55,10 +55,10 @@ import AndroidSection from "./android-section";import CloakingSection from "./cloaking-section";import CommentsSection from "./comments-section";import ConversionSection from "./conversion-section";+import DoIndexSection from "./doindex-section";import ExpirationSection from "./expiration-section";import GeoSection from "./geo-section";import IOSSection from "./ios-section";-import NoindexSection from "./noindex-section";import OGSection from "./og-section";import PasswordSection from "./password-section";import Preview from "./preview";@@ -750,7 +750,7 @@ function AddEditLinkModal({<IOSSection {...{ props, data, setData }} /><AndroidSection {...{ props, data, setData }} /><GeoSection {...{ props, data, setData }} />- <NoindexSection {...{ data, setData }} />+ <DoIndexSection {...{ data, setData }} /><CommentsSection {...{ props, data, setData }} /></div>diff --git a/packages/utils/src/constants/misc.ts b/packages/utils/src/constants/misc.tsindex d79589975c..2b43d97dab 100644--- a/packages/utils/src/constants/misc.ts+++ b/packages/utils/src/constants/misc.ts@@ -15,7 +15,7 @@ export const DEFAULT_LINK_PROPS: any = {password: null,ios: null,android: null,- noindex: true,+ doIndex: false,clicks: 0,userId: "",