The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Added rewrite option for root domain
Steven Tey2264841fc6ab
diff --git a/README.md b/README.mdindex 454481d346..29dd20a732 100644--- a/README.md+++ b/README.md@@ -58,6 +58,8 @@ Add a custom OG image in front of your target URL. Bots like Twitter/Facebook wi## Deploy Your Own+> Note: one-click deployment is a bit broken at the moment – you'll need to change some of the hard-coded values in the codebase to get it working. We're working on fixing this.+You can deploy your own hosted version of Dub for greater privacy & control. Just click the link below to deploy a ready-to-go version of Dub to Vercel.[](https://dub.sh/deploy)diff --git a/lib/middleware/root.ts b/lib/middleware/root.tsindex 2bd84f4a3b..9380b46855 100644--- a/lib/middleware/root.ts+++ b/lib/middleware/root.ts@@ -3,6 +3,7 @@ import { redis } from "@/lib/upstash";import { HOME_HOSTNAMES } from "@/lib/constants";import { recordClick } from "@/lib/tinybird";import { parse } from "./utils";+import { RootDomainProps } from "../types";export default async function RootMiddleware(req: NextRequest,@@ -25,13 +26,17 @@ export default async function RootMiddleware() {return NextResponse.next();} else {- const target = await redis.get<string>(`root:${domain}`);+ const { target, rewrite } =+ (await redis.get<RootDomainProps>(`root:${domain}`)) || {};if (target) {- return NextResponse.redirect(target);+ if (rewrite) {+ return NextResponse.rewrite(target);+ } else {+ return NextResponse.redirect(target);+ }} else {- const url = req.nextUrl;- url.pathname = `/placeholder/${domain}`; // rewrite to a /placeholder page unless the user defines a site to redirect to- return NextResponse.rewrite(url);+ // rewrite to a /placeholder page unless the user defines a site to redirect to+ return NextResponse.rewrite(new URL(`/placeholder/${domain}`, req.url));}}}diff --git a/lib/types.ts b/lib/types.tsindex cb3088caf3..bbb1c4f654 100644--- a/lib/types.ts+++ b/lib/types.ts@@ -65,3 +65,8 @@ export type DomainVerificationStatusProps =| "Pending Verification"| "Domain Not Found"| "Unknown Error";++export interface RootDomainProps {+ target: string;+ rewrite?: boolean;+}diff --git a/pages/api/projects/[slug]/domains/[domain]/root/index.ts b/pages/api/projects/[slug]/domains/[domain]/root/index.tsindex 3cb065ab5a..91cb348fa0 100644--- a/pages/api/projects/[slug]/domains/[domain]/root/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/root/index.ts@@ -1,6 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import { withProjectAuth } from "@/lib/auth";import { redis } from "@/lib/upstash";+import { RootDomainProps } from "@/lib/types";export default withProjectAuth(async (req: NextApiRequest, res: NextApiResponse) => {@@ -8,13 +9,17 @@ export default withProjectAuth(// GET /api/projects/[slug]/domains/[domain]/root - get root domainif (req.method === "GET") {- const rootDomain = await redis.get<string>(`root:${domain}`);- return res.status(200).json(rootDomain);+ const { target } =+ (await redis.get<RootDomainProps>(`root:${domain}`)) || {};+ return res.status(200).json(target);// PUT /api/projects/[slug]/domains/[domain]/root - change root domain} else if (req.method === "PUT") {const { rootDomain } = req.body as { rootDomain: string };- const response = await redis.set(`root:${domain}`, rootDomain);+ const response = await redis.set(`root:${domain}`, {+ target: rootDomain,+ rewrite: false,+ });res.status(200).json(response);} else {res.setHeader("Allow", ["GET", "PUT"]);