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.

Added link inspector (closes #22)

Steven Tey52bd97be3525
diff --git a/app/admin/page.tsx b/app/admin/page.tsx
index b1c93abc48..ef9c1344f9 100644
--- a/app/admin/page.tsx
+++ b/app/admin/page.tsx
@@ -5,6 +5,7 @@ import BanUser from "./components/ban";
export const metadata = constructMetadata({
title: "Dub Admin",
+ noIndex: true,
});
export default function AdminPage() {
diff --git a/app/inspect/[domain]/[key]/page.tsx b/app/inspect/[domain]/[key]/page.tsx
new file mode 100644
index 0000000000..071ad540de
--- /dev/null
+++ b/app/inspect/[domain]/[key]/page.tsx
@@ -0,0 +1,107 @@
+import { GOOGLE_FAVICON_URL } from "#/lib/constants";
+import { getLinkViaEdge } from "#/lib/planetscale";
+import { constructMetadata, getApexDomain, linkConstructor } from "#/lib/utils";
+import Footer from "#/ui/home/footer";
+import Nav from "#/ui/home/nav";
+import CopyButton from "@/components/shared/copy-button";
+import { notFound } from "next/navigation";
+import LinkPreview from "./preview";
+import Background from "#/ui/home/background";
+import BlurImage from "#/ui/blur-image";
+import Script from "next/script";
+import ReportButton from "./report";
+
+export const runtime = "edge";
+
+export async function generateMetadata({
+ params,
+}: {
+ params: { domain: string; key: string };
+}) {
+ const domain = params.domain;
+ const key = decodeURIComponent(params.key).slice(0, -1);
+
+ const data = await getLinkViaEdge(domain, key);
+
+ if (!data) {
+ return;
+ }
+
+ return constructMetadata({
+ noIndex: true,
+ });
+}
+
+export default async function InspectPage({
+ params,
+}: {
+ params: { domain: string; key: string };
+}) {
+ const domain = params.domain;
+ const key = decodeURIComponent(params.key).slice(0, -1);
+
+ const data = await getLinkViaEdge(domain, key);
+
+ // if the link doesn't exist
+ if (!data) {
+ notFound();
+ }
+
+ const apexDomain = getApexDomain(data.url);
+
+ return (
+ <>
+ <Script src="https://tally.so/widgets/embed.js" strategy="lazyOnload" />
+ <main className="flex min-h-screen flex-col justify-between">
+ <Nav />
+ <div className="z-10 mx-2 my-10 flex max-w-md flex-col space-y-5 px-2.5 text-center sm:mx-auto sm:max-w-lg sm:px-0 lg:mb-16">
+ <h1 className="font-display text-5xl font-extrabold leading-[1.15] text-black sm:text-6xl sm:leading-[1.15]">
+ Link Inspector
+ </h1>
+ <h2 className="text-lg text-gray-600 sm:text-xl">
+ Inspect a short link on Dub to make sure it's safe to click on. If
+ you think this link is malicious, please report it.
+ </h2>
+
+ <div className="flex w-full items-center justify-between rounded-md border border-gray-300 bg-white p-3">
+ <div className="flex items-center space-x-3">
+ <BlurImage
+ src={`${GOOGLE_FAVICON_URL}${apexDomain}`}
+ alt={apexDomain}
+ className="pointer-events-none h-10 w-10 rounded-full"
+ width={20}
+ height={20}
+ />
+ <div>
+ <div className="flex items-center space-x-1 sm:space-x-2">
+ <a
+ className="font-semibold text-blue-800"
+ href={linkConstructor({ domain, key })}
+ target="_blank"
+ rel="noreferrer"
+ >
+ {linkConstructor({ domain, key, pretty: true })}
+ </a>
+ <CopyButton url={linkConstructor({ domain, key })} />
+ </div>
+ <a
+ href={data.url}
+ target="_blank"
+ rel="noopener noreferrer"
+ className="line-clamp-1 text-left text-sm text-gray-500 underline-offset-2 transition-all hover:text-gray-800 hover:underline"
+ >
+ {data.url}
+ </a>
+ </div>
+ </div>
+ <ReportButton link={linkConstructor({ domain, key })} />
+ </div>
+
+ <LinkPreview url={data.url} />
+ </div>
+ <Footer />
+ <Background />
+ </main>
+ </>
+ );
+}
diff --git a/app/inspect/[domain]/[key]/preview.tsx b/app/inspect/[domain]/[key]/preview.tsx
new file mode 100644
index 0000000000..08462f96b3
--- /dev/null
+++ b/app/inspect/[domain]/[key]/preview.tsx
@@ -0,0 +1,59 @@
+"use client";
+import { fetcher, getDomainWithoutWWW } from "#/lib/utils";
+import { LoadingCircle } from "#/ui/icons";
+import { ImageOff } from "lucide-react";
+import useSWR from "swr";
+
+export default function LinkPreview({ url }: { url: string }) {
+ const { data, isValidating } = useSWR<{
+ title: string | null;
+ description: string | null;
+ image: string | null;
+ }>(`/api/edge/metatags?url=${url}`, fetcher, {
+ revalidateOnFocus: false,
+ });
+
+ const { title, description, image } = data || {};
+
+ const hostname = getDomainWithoutWWW(url);
+
+ return (
+ <div className="relative overflow-hidden rounded-md border border-gray-300 bg-gray-50">
+ {isValidating && (
+ <div className="absolute flex h-[250px] w-full flex-col items-center justify-center space-y-4 border-b border-gray-300 bg-gray-50">
+ <LoadingCircle />
+ </div>
+ )}
+ {image ? (
+ <img
+ src={image}
+ alt="Preview"
+ className="h-[250px] w-full border-b border-gray-300 object-cover"
+ />
+ ) : (
+ <div className="flex h-[250px] w-full flex-col items-center justify-center space-y-4 border-b border-gray-300">
+ <ImageOff className="h-8 w-8 text-gray-400" />
+ <p className="text-sm text-gray-400">No OG image found.</p>
+ </div>
+ )}
+ <div className="grid gap-1 bg-white p-3 text-left">
+ <p className="text-sm text-[#536471]">{hostname}</p>
+ {title ? (
+ <h3 className="truncate text-sm font-medium text-[#0f1419]">
+ {title}
+ </h3>
+ ) : (
+ <div className="mb-1 h-4 w-full rounded-md bg-gray-100" />
+ )}
+ {description ? (
+ <p className="line-clamp-2 text-sm text-[#536471]">{description}</p>
+ ) : (
+ <div className="grid gap-2">
+ <div className="h-4 w-full rounded-md bg-gray-100" />
+ <div className="h-4 w-48 rounded-md bg-gray-100" />
+ </div>
+ )}
+ </div>
+ </div>
+ );
+}
diff --git a/app/inspect/[domain]/[key]/report.tsx b/app/inspect/[domain]/[key]/report.tsx
new file mode 100644
index 0000000000..da77220cdc
--- /dev/null
+++ b/app/inspect/[domain]/[key]/report.tsx
@@ -0,0 +1,39 @@
+"use client";
+import { cn } from "#/lib/utils";
+import { LoadingSpinner } from "#/ui/icons";
+import { Flag } from "lucide-react";
+import { useState } from "react";
+
+export default function ReportButton({ link }: { link: string }) {
+ const [opening, setOpening] = useState(false);
+ return (
+ <button
+ type="button"
+ onClick={() => {
+ setOpening(true);
+ // @ts-ignore
+ window.Tally?.openPopup("mJpe4J", {
+ width: 540,
+ hiddenFields: {
+ link,
+ },
+ onOpen: () => setOpening(false),
+ });
+ }}
+ disabled={opening}
+ className={cn(
+ "rounded-md p-2 transition-all duration-75 focus:outline-none",
+ opening
+ ? "cursor-not-allowed bg-gray-100"
+ : "hover:bg-red-100 active:bg-red-200",
+ )}
+ >
+ <span className="sr-only">Report</span>
+ {opening ? (
+ <LoadingSpinner className="h-4 w-4" />
+ ) : (
+ <Flag className="h-4 w-4 text-red-500" />
+ )}
+ </button>
+ );
+}
diff --git a/app/protected/[domain]/[key]/page.tsx b/app/protected/[domain]/[key]/page.tsx
index 04470a3c00..2829d35dc7 100644
--- a/app/protected/[domain]/[key]/page.tsx
+++ b/app/protected/[domain]/[key]/page.tsx
@@ -12,6 +12,7 @@ export const metadata = constructMetadata({
title,
description,
image,
+ noIndex: true,
});
export default function PasswordProtectedLinkPage() {
diff --git a/app/proxy/[domain]/[key]/page.tsx b/app/proxy/[domain]/[key]/page.tsx
index 3fbff4a013..91ff687426 100644
--- a/app/proxy/[domain]/[key]/page.tsx
+++ b/app/proxy/[domain]/[key]/page.tsx
@@ -24,6 +24,7 @@ export async function generateMetadata({
description: unescape(data.description),
image: unescape(data.image),
icons: `${GOOGLE_FAVICON_URL}${unescape(apexDomain)}`,
+ noIndex: true,
});
}
diff --git a/components/shared/copy-button.tsx b/components/shared/copy-button.tsx
index a324c8e15a..c8a812d86d 100644
--- a/components/shared/copy-button.tsx
+++ b/components/shared/copy-button.tsx
@@ -1,3 +1,5 @@
+"use client";
+
import { useState } from "react";
import { Copy, Tick } from "@/components/shared/icons";
import { toast } from "sonner";
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index a45d48208e..f212be038d 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -39,8 +39,7 @@ export default async function LinkMiddleware(
}
}
- const { country } =
- process.env.VERCEL === "1" && req.geo ? req.geo : LOCALHOST_GEO_DATA;
+ const inspectMode = key.endsWith("+");
const response = await redis.get<{
url: string;
@@ -50,7 +49,7 @@ export default async function LinkMiddleware(
ios?: string;
android?: string;
geo?: object;
- }>(`${domain}:${key}`);
+ }>(`${domain}:${inspectMode ? key.slice(0, -1) : key}`);
const {
url: target,
@@ -63,6 +62,12 @@ export default async function LinkMiddleware(
} = response || {};
if (target) {
+ if (inspectMode) {
+ return NextResponse.rewrite(
+ new URL(`/inspect/${domain}/${key}`, req.url),
+ );
+ }
+
// special case for link health monitoring with planetfall.io :)
if (!req.headers.get("dub-no-track")) {
ev.waitUntil(recordClick(domain, req, key)); // track the click only if there is no `dub-no-track` header
@@ -75,6 +80,9 @@ export default async function LinkMiddleware(
);
}
+ const { country } =
+ process.env.VERCEL === "1" && req.geo ? req.geo : LOCALHOST_GEO_DATA;
+
const isBot = detectBot(req);
if (isBot && proxy) {
// rewrite to proxy page (/_proxy/[domain]/[key]) if it's a bot
diff --git a/lib/middleware/utils.ts b/lib/middleware/utils.ts
index 44ef669234..a3b67a9641 100644
--- a/lib/middleware/utils.ts
+++ b/lib/middleware/utils.ts
@@ -3,6 +3,7 @@ import { NextRequest } from "next/server";
export const parse = (req: NextRequest) => {
let domain = req.headers.get("host") as string;
domain = domain.replace("www.", ""); // remove www. from domain
+ if (domain === "dub.localhost:8888") domain = "dub.sh"; // for local development
// path is the path of the URL (e.g. dub.co/stats/github -> /stats/github)
const path = req.nextUrl.pathname;
diff --git a/lib/utils.ts b/lib/utils.ts
index d663697aac..9368116ce9 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -22,11 +22,13 @@ export function constructMetadata({
description = "Dub is an open-source link management tool for modern marketing teams to create, share, and track short links.",
image = "https://dub.co/_static/thumbnail.png",
icons = "/favicon.ico",
+ noIndex = false,
}: {
title?: string;
description?: string;
image?: string;
icons?: string;
+ noIndex?: boolean;
} = {}): Metadata {
return {
title,
@@ -50,6 +52,12 @@ export function constructMetadata({
icons,
metadataBase: new URL(HOME_DOMAIN),
themeColor: "#FFF",
+ ...(noIndex && {
+ robots: {
+ index: false,
+ follow: false,
+ },
+ }),
};
}