The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add support for noindex on root domain
Steven Tey0b1e078413ce
diff --git a/apps/web/app/api/callback/stripe/route.ts b/apps/web/app/api/callback/stripe/route.tsindex 75691bb26d..de206438e0 100644--- a/apps/web/app/api/callback/stripe/route.ts+++ b/apps/web/app/api/callback/stripe/route.ts@@ -259,6 +259,17 @@ export const POST = async (req: Request) => {text: "Hey!\n\nI noticed you recently cancelled your Dub.co subscription – we're sorry to see you go!\n\nI'd love to hear your feedback on your experience with Dub – what could we have done better?\n\nThanks!\n\nSteven Tey\nFounder, Dub.co",}),),+ workspace.domains.forEach((domain) => {+ prisma.domain.update({+ where: {+ id: domain.id,+ },+ data: {+ target: null,+ noindex: false,+ },+ });+ }),]);}} catch (error) {diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.tsindex 3267a71161..afd3916d49 100644--- a/apps/web/app/api/domains/[domain]/route.ts+++ b/apps/web/app/api/domains/[domain]/route.ts@@ -61,6 +61,7 @@ export const PATCH = withWorkspace(placeholder,expiredUrl,archived,+ noindex,} = updateDomainBodySchema.parse(body);if (newDomain && newDomain !== domain) {@@ -92,6 +93,7 @@ export const PATCH = withWorkspace(...(workspace.plan !== "free" && {target,expiredUrl,+ noindex,}),},});@@ -104,6 +106,7 @@ export const PATCH = withWorkspace(domainCreatedAt: response.createdAt,...(workspace.plan !== "free" && {url: target || undefined,+ noindex,}),rewrite: type === "rewrite",...(newDomain !== domain && {diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.tsindex e7244632c3..888377c3b2 100644--- a/apps/web/app/api/domains/route.ts+++ b/apps/web/app/api/domains/route.ts@@ -32,6 +32,7 @@ export const POST = withWorkspace(async ({ req, workspace }) => {type,expiredUrl,placeholder,+ noindex,} = addDomainBodySchema.parse(body);if (workspace.domains.length >= workspace.domainsLimit) {@@ -74,6 +75,7 @@ export const POST = withWorkspace(async ({ req, workspace }) => {...(workspace.plan !== "free" && {target,expiredUrl,+ noindex,}),},});@@ -86,6 +88,7 @@ export const POST = withWorkspace(async ({ req, workspace }) => {projectId: workspace.id,...(workspace.plan !== "free" && {url: target || undefined,+ noindex,}),rewrite: type === "rewrite",}),diff --git a/apps/web/app/robots.ts b/apps/web/app/robots.tsindex 79c5624c10..ab0df642b9 100644--- a/apps/web/app/robots.ts+++ b/apps/web/app/robots.ts@@ -1,4 +1,3 @@-import { SHORT_DOMAIN, isDubDomain } from "@dub/utils";import { MetadataRoute } from "next";import { headers } from "next/headers";@@ -6,33 +5,11 @@ export default function robots(): MetadataRoute.Robots {const headersList = headers();let domain = headersList.get("host") as string;- if (domain === "dub.localhost:8888" || domain.endsWith(".vercel.app")) {- // for local development and preview URLs- domain = SHORT_DOMAIN;- }-- if (isDubDomain(domain)) {- return {- rules: [- {- userAgent: "*",- allow: "/",- },- ],- sitemap: `https://${domain}/sitemap.xml`,- };- }-return {rules: [{- userAgent: "Googlebot", // for Googlebot- allow: ["/$", "/api/og/"], // allow the home page and the OG image API- disallow: "/", // disallow everything else- },- {- userAgent: "LinkedInBot", // for LinkedInBot- allow: "/", // allow everything+ userAgent: "*",+ allow: "/",},],sitemap: `https://${domain}/sitemap.xml`,diff --git a/apps/web/lib/api/domains/set-root-domain.ts b/apps/web/lib/api/domains/set-root-domain.tsindex 1d594043cc..b0c2bdfee9 100644--- a/apps/web/lib/api/domains/set-root-domain.ts+++ b/apps/web/lib/api/domains/set-root-domain.ts@@ -9,6 +9,7 @@ export async function setRootDomain({projectId,url,rewrite,+ noindex,newDomain,}: {id: string;@@ -17,6 +18,7 @@ export async function setRootDomain({projectId: string;url?: string;rewrite?: boolean;+ noindex?: boolean;newDomain?: string; // if the domain is changed, this will be the new domain}) {if (newDomain) {@@ -39,6 +41,9 @@ export async function setRootDomain({: domain.toLowerCase(),}),}),+ ...(noindex && {+ noindex: true,+ }),projectId,},}),diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.tsindex 45a16e67d0..7703af19cf 100644--- a/apps/web/lib/middleware/link.ts+++ b/apps/web/lib/middleware/link.ts@@ -6,6 +6,7 @@ import {DUB_HEADERS,LEGAL_WORKSPACE_ID,LOCALHOST_GEO_DATA,+ isDubDomain,punyEncode,} from "@dub/utils";import {@@ -60,7 +61,10 @@ export default async function LinkMiddleware(// short link not found, redirect to root// TODO: log 404s (https://github.com/dubinc/dub/issues/559)return NextResponse.redirect(new URL("/", req.url), {- ...DUB_HEADERS,+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },status: 302,});}@@ -93,7 +97,12 @@ export default async function LinkMiddleware(if (inspectMode && !password) {return NextResponse.rewrite(new URL(`/inspect/${domain}/${encodeURIComponent(key)}+`, req.url),- DUB_HEADERS,+ {+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ },);}@@ -108,7 +117,14 @@ export default async function LinkMiddleware(if (!pw || (await getLinkViaEdge(domain, key))?.password !== pw) {return NextResponse.rewrite(new URL(`/password/${domain}/${encodeURIComponent(key)}`, req.url),- DUB_HEADERS,+ {+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && {+ "X-Robots-Tag": "googlebot: noindex",+ }),+ },+ },);} else if (pw) {// strip it from the URL if it's correct@@ -118,18 +134,30 @@ export default async function LinkMiddleware(// if the link is bannedif (link.projectId === LEGAL_WORKSPACE_ID) {- return NextResponse.rewrite(new URL("/banned", req.url), DUB_HEADERS);+ return NextResponse.rewrite(new URL("/banned", req.url), {+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ });}// if the link has expiredif (expiresAt && new Date(expiresAt) < new Date()) {if (expiredUrl) {- return NextResponse.redirect(expiredUrl, DUB_HEADERS);+ return NextResponse.redirect(expiredUrl, {+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ });} else {- return NextResponse.rewrite(- new URL(`/expired/${domain}`, req.url),- DUB_HEADERS,- );+ return NextResponse.rewrite(new URL(`/expired/${domain}`, req.url), {+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ });}}@@ -153,14 +181,24 @@ export default async function LinkMiddleware(if (isBot && proxy) {return NextResponse.rewrite(new URL(`/proxy/${domain}/${encodeURIComponent(key)}`, req.url),- DUB_HEADERS,+ {+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ },);// rewrite to mailto page if the link is a mailto link} else if (url.startsWith("mailto:")) {return NextResponse.rewrite(new URL(`/mailto/${encodeURIComponent(url)}`, req.url),- DUB_HEADERS,+ {+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ },);// rewrite to target URL if link cloaking is enabled@@ -168,38 +206,62 @@ export default async function LinkMiddleware(if (iframeable) {return NextResponse.rewrite(new URL(`/cloaked/${encodeURIComponent(url)}`, req.url),- DUB_HEADERS,+ {+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && {+ "X-Robots-Tag": "googlebot: noindex",+ }),+ },+ },);} else {// if link is not iframeable, use Next.js rewrite instead- return NextResponse.rewrite(url, DUB_HEADERS);+ return NextResponse.rewrite(url, {+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ });}// redirect to iOS link if it is specified and the user is on an iOS device} else if (ios && userAgent(req).os?.name === "iOS") {return NextResponse.redirect(getFinalUrl(ios, { req }), {- ...DUB_HEADERS,+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },status: 302,});// redirect to Android link if it is specified and the user is on an Android device} else if (android && userAgent(req).os?.name === "Android") {return NextResponse.redirect(getFinalUrl(android, { req }), {- ...DUB_HEADERS,+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },status: 302,});// redirect to geo-specific link if it is specified and the user is in the specified country} else if (geo && country && country in geo) {return NextResponse.redirect(getFinalUrl(geo[country], { req }), {- ...DUB_HEADERS,+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },status: 302,});// regular redirect} else {return NextResponse.redirect(getFinalUrl(url, { req }), {- ...DUB_HEADERS,+ headers: {+ ...DUB_HEADERS,+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),+ },status: 302,});}diff --git a/apps/web/lib/middleware/root.ts b/apps/web/lib/middleware/root.tsindex 0713886f14..cf63466475 100644--- a/apps/web/lib/middleware/root.ts+++ b/apps/web/lib/middleware/root.ts@@ -23,7 +23,11 @@ export default async function RootMiddleware(if (!linkData) {// rewrite to placeholder page if domain doesn't exist- return NextResponse.rewrite(new URL(`/${domain}`, req.url), DUB_HEADERS);+ return NextResponse.rewrite(new URL(`/${domain}`, req.url), {+ headers: {+ ...DUB_HEADERS,+ },+ });}// format link to fit the RedisLinkProps interface@@ -36,28 +40,48 @@ export default async function RootMiddleware();}- const { id, url, rewrite, iframeable } = link;+ const { id, url, rewrite, iframeable, noindex } = link;// record clicks on root pageev.waitUntil(recordClick({ req, id, ...(url && { url }), root: true }));if (!url) {// rewrite to placeholder page unless the user defines a site to redirect to- return NextResponse.rewrite(new URL(`/${domain}`, req.url), DUB_HEADERS);+ return NextResponse.rewrite(new URL(`/${domain}`, req.url), {+ headers: {+ ...DUB_HEADERS,+ },+ });}if (rewrite) {if (iframeable) {return NextResponse.rewrite(new URL(`/cloaked/${encodeURIComponent(url)}`, req.url),- DUB_HEADERS,+ {+ headers: {+ ...DUB_HEADERS,+ ...(noindex && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ },);} else {// if link is not iframeable, use Next.js rewrite instead- return NextResponse.rewrite(url, DUB_HEADERS);+ return NextResponse.rewrite(url, {+ headers: {+ ...DUB_HEADERS,+ ...(noindex && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ });}} else {// For root links that have a destination URL, use 301 status code (for SEO purposes)- return NextResponse.redirect(url, { ...DUB_HEADERS, status: 301 });+ return NextResponse.redirect(url, {+ headers: {+ ...DUB_HEADERS,+ ...(noindex && { "X-Robots-Tag": "googlebot: noindex" }),+ },+ status: 301,+ });}}diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex 63063eab98..9d4b355dd5 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -132,6 +132,7 @@ export interface DomainProps {clicks: number;projectId: string;expiredUrl?: string;+ noindex?: boolean;}export interface RedisDomainProps {id: string;@@ -139,6 +140,7 @@ export interface RedisDomainProps {rewrite?: boolean;iframeable?: boolean;projectId: string;+ noindex?: boolean;}export interface BitlyGroupProps {diff --git a/apps/web/lib/upstash.ts b/apps/web/lib/upstash.tsindex 67701c4054..7178bbbbbd 100644--- a/apps/web/lib/upstash.ts+++ b/apps/web/lib/upstash.ts@@ -101,7 +101,7 @@ export async function formatRedisLink(export async function formatRedisDomain(domain: DomainProps,): Promise<RedisDomainProps> {- const { id, slug, target: url, type, projectId } = domain;+ const { id, slug, target: url, type, projectId, noindex } = domain;return {id,@@ -112,5 +112,6 @@ export async function formatRedisDomain(iframeable: await isIframeable({ url, requestDomain: slug }),}),projectId,+ ...(noindex && { noindex: true }),};}diff --git a/apps/web/lib/zod/schemas/domains.ts b/apps/web/lib/zod/schemas/domains.tsindex 323e389982..860c009134 100644--- a/apps/web/lib/zod/schemas/domains.ts+++ b/apps/web/lib/zod/schemas/domains.ts@@ -19,6 +19,10 @@ export const DomainSchema = z.object({.boolean().describe("Whether the domain is archived.").default(false),+ noindex: z+ .boolean()+ .describe("Prevent search engines from indexing the domain.")+ .default(false),placeholder: z.string().describe(@@ -79,6 +83,13 @@ export const addDomainBodySchema = z.object({"Whether to archive this domain. `false` will unarchive a previously archived domain.",).openapi({ example: false }),+ noindex: z+ .boolean()+ .optional()+ .default(false)+ .describe(+ "Prevent search engines from indexing the domain. Defaults to `false`.",+ ),placeholder: parseUrlSchema.nullish().default("https://dub.co/help/article/what-is-dub")diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prismaindex 0b3de0020f..6f1ecc9ece 100644--- a/apps/web/prisma/schema.prisma+++ b/apps/web/prisma/schema.prisma@@ -181,6 +181,7 @@ model Domain {primary Boolean @default(false)archived Boolean @default(false)expiredUrl String? @db.LongText // URL to redirect to for expired links+ noindex Boolean @default(false)lastChecked DateTime @default(now())links Link[]project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)diff --git a/apps/web/ui/modals/add-edit-domain-modal/index.tsx b/apps/web/ui/modals/add-edit-domain-modal/index.tsxindex a9eb449f1e..c9cc0c8b9a 100644--- a/apps/web/ui/modals/add-edit-domain-modal/index.tsx+++ b/apps/web/ui/modals/add-edit-domain-modal/index.tsx@@ -9,6 +9,7 @@ import {Logo,Modal,SimpleTooltipContent,+ Switch,Tooltip,TooltipContent,useRouterStuff,@@ -355,6 +356,35 @@ function AddEditDomainModal({/></div></div>++ <div className="flex w-full items-center justify-between">+ <div className="flex items-center space-x-2">+ <h2 className="text-sm font-medium text-gray-900">+ Disable Search Engine Indexing+ </h2>+ <InfoTooltip content="Prevent search engines from indexing your root domain." />+ </div>+ <Switch+ checked={data.noindex}+ fn={(checked) => setData({ ...data, noindex: checked })}+ {...(plan === "free" && {+ disabledTooltip: (+ <TooltipContent+ title="You can only disable search engine indexing on a Pro plan and above. Upgrade to proceed."+ cta="Upgrade to Pro"+ onClick={() => {+ setShowAddEditDomainModal(false);+ queryParams({+ set: {+ upgrade: "pro",+ },+ });+ }}+ />+ ),+ })}+ />+ </div></motion.div>)}diff --git a/packages/utils/src/constants/middleware.ts b/packages/utils/src/constants/middleware.tsindex d602ca812b..c1f230f725 100644--- a/packages/utils/src/constants/middleware.ts+++ b/packages/utils/src/constants/middleware.ts@@ -14,7 +14,5 @@ export const DEFAULT_REDIRECTS = {};export const DUB_HEADERS = {- headers: {- "x-powered-by": "Dub.co - Link management for modern marketing teams",- },+ "x-powered-by": "Dub.co - Link management for modern marketing teams",};