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.

converted OG proxy pages to App Router + edge runtime

Steven Tey9f02b197537d
diff --git a/app/[domain]/stats/[key]/page.tsx b/app/[domain]/stats/[key]/page.tsx
index 71439b0d04..b8b6fc65ea 100644
--- a/app/[domain]/stats/[key]/page.tsx
+++ b/app/[domain]/stats/[key]/page.tsx
@@ -14,7 +14,7 @@ export async function generateMetadata({
const data = await getLinkViaEdge(params.domain, params.key);
// if the link doesn't exist or is explicitly private (publicStats === false)
- if (!data || data?.publicStats === 0) {
+ if (!data || data.publicStats === 0) {
return;
}
@@ -34,7 +34,7 @@ export default async function StatsPage({
}) {
const data = await getLinkViaEdge(params.domain, params.key);
- if (!data || data?.publicStats === 0) {
+ if (!data || data.publicStats === 0) {
notFound();
}
diff --git a/app/dub.sh/page.tsx b/app/dub.sh/page.tsx
index bea6dfb775..04eab32b48 100644
--- a/app/dub.sh/page.tsx
+++ b/app/dub.sh/page.tsx
@@ -6,11 +6,8 @@ import Hero from "#/ui/home/hero";
import Logos from "#/ui/home/logos";
import OSS from "#/ui/home/oss";
import Testimonials from "#/ui/home/testimonials";
-import { constructMetadata } from "#/lib/utils";
import Changelog from "#/ui/home/changelog";
-export const metadata = constructMetadata({});
-
export default function Home() {
return (
<>
diff --git a/app/layout.tsx b/app/layout.tsx
index 7c2a73ce8b..38ddc92f10 100644
--- a/app/layout.tsx
+++ b/app/layout.tsx
@@ -3,7 +3,7 @@ import { satoshi, inter } from "@/styles/fonts";
import { cn, constructMetadata } from "#/lib/utils";
import Providers from "./providers";
-export const metadata = constructMetadata({});
+export const metadata = constructMetadata();
export default function RootLayout({
children,
diff --git a/app/proxy/[domain]/[key]/page.tsx b/app/proxy/[domain]/[key]/page.tsx
new file mode 100644
index 0000000000..3fbff4a013
--- /dev/null
+++ b/app/proxy/[domain]/[key]/page.tsx
@@ -0,0 +1,72 @@
+import { getLinkViaEdge } from "#/lib/planetscale";
+import { unescape } from "html-escaper";
+import { constructMetadata, getApexDomain } from "#/lib/utils";
+import { notFound, redirect } from "next/navigation";
+import { GOOGLE_FAVICON_URL } from "#/lib/constants";
+
+export const runtime = "edge";
+
+export async function generateMetadata({
+ params,
+}: {
+ params: { domain: string; key: string };
+}) {
+ const data = await getLinkViaEdge(params.domain, params.key);
+
+ if (!data || data.proxy === 0) {
+ return;
+ }
+
+ const apexDomain = getApexDomain(data.url);
+
+ return constructMetadata({
+ title: unescape(data.title),
+ description: unescape(data.description),
+ image: unescape(data.image),
+ icons: `${GOOGLE_FAVICON_URL}${unescape(apexDomain)}`,
+ });
+}
+
+export default async function ProxyOGPage({
+ params,
+}: {
+ params: { domain: string; key: string };
+}) {
+ const data = await getLinkViaEdge(params.domain, params.key);
+
+ // if the link doesn't exist
+ if (!data) {
+ notFound();
+
+ // if the link does not have proxy enabled, redirect to the original URL
+ } else if (data.proxy === 0) {
+ redirect(data.url);
+ }
+
+ const apexDomain = getApexDomain(data.url);
+
+ return (
+ <main className="flex h-screen w-screen items-center justify-center">
+ <div className="mx-5 w-full max-w-lg overflow-hidden rounded-lg border border-gray-200 sm:mx-0">
+ <img
+ src={unescape(data.image)}
+ alt={unescape(data.title)}
+ className="w-full object-cover"
+ />
+ <div className="flex space-x-3 bg-gray-100 p-5">
+ <img
+ src={`${GOOGLE_FAVICON_URL}${unescape(apexDomain)}`}
+ alt={unescape(data.title)}
+ className="mt-1 h-6 w-6"
+ />
+ <div className="flex flex-col space-y-3">
+ <h1 className="font-bold text-gray-700">{unescape(data.title)}</h1>
+ <p className="text-sm text-gray-500">
+ {unescape(data.description)}
+ </p>
+ </div>
+ </div>
+ </div>
+ </main>
+ );
+}
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index 58b017678f..970bd5ff5c 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -44,7 +44,7 @@ export default async function LinkMiddleware(
const isBot = detectBot(req);
if (isBot && proxy) {
// rewrite to proxy page (/_proxy/[domain]/[key]) if it's a bot
- return NextResponse.rewrite(new URL(`/_proxy/${domain}/${key}`, req.url));
+ return NextResponse.rewrite(new URL(`/proxy/${domain}/${key}`, req.url));
} else if (ios && userAgent(req).os?.name === "iOS") {
// redirect to iOS link if it is specified and the user is on an iOS device
return NextResponse.redirect(getFinalUrl(ios, { req }), REDIRECT_HEADERS);
diff --git a/lib/planetscale.ts b/lib/planetscale.ts
index 75c9fd645b..eb316d9dcf 100644
--- a/lib/planetscale.ts
+++ b/lib/planetscale.ts
@@ -11,7 +11,7 @@ export const getLinkViaEdge = async (domain: string, key: string) => {
const { rows } =
(await conn.execute(
- "SELECT `key`, url, clicks, userId, publicStats FROM Link WHERE domain = ? AND `key` = ?",
+ "SELECT `key`, url, proxy, title, description, image, clicks, userId, publicStats FROM Link WHERE domain = ? AND `key` = ?",
[domain, key],
)) || {};
@@ -19,6 +19,10 @@ export const getLinkViaEdge = async (domain: string, key: string) => {
? (rows[0] as {
key: string;
url: string;
+ proxy: number;
+ title: string;
+ description: string;
+ image: string;
clicks: number;
userId: number;
publicStats: number;
diff --git a/lib/utils.ts b/lib/utils.ts
index 2f9d706cf7..bb3fa08c37 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -20,11 +20,13 @@ export function constructMetadata({
title = "Dub - Link Management for Modern Marketing Teams",
description = "Dub is an open-source link management tool for modern marketing teams to create, share, and track short links.",
image = "https://dub.sh/_static/thumbnail.png",
+ icons = "/favicon.ico",
}: {
title?: string;
description?: string;
image?: string;
-}): Metadata {
+ icons?: string;
+} = {}): Metadata {
return {
title,
description,
@@ -44,7 +46,7 @@ export function constructMetadata({
images: [image],
creator: "@dubdotsh",
},
- icons: "/favicon.ico",
+ icons,
metadataBase: new URL("https://dub.sh"),
themeColor: "#FFF",
};
diff --git a/pages/_proxy/[domain]/[key].tsx b/pages/_proxy/[domain]/[key].tsx
deleted file mode 100644
index b84449abcb..0000000000
--- a/pages/_proxy/[domain]/[key].tsx
+++ /dev/null
@@ -1,100 +0,0 @@
-import Head from "next/head";
-import { unescape } from "html-escaper";
-import prisma from "#/lib/prisma";
-import { getApexDomain } from "#/lib/utils";
-import { GOOGLE_FAVICON_URL } from "#/lib/constants";
-
-export default function LinkPage({
- shortLink,
- fullDomain,
- apexDomain,
- title,
- description,
- image,
-}) {
- return (
- <>
- <Head>
- <meta property="og:title" content={unescape(title)} />
- <meta property="og:site_name" content={unescape(fullDomain)} />
- <meta property="og:description" content={unescape(description)} />
- <meta property="og:image" content={unescape(image)} />
- <meta
- property="og:image:alt"
- content={`OG image for ${unescape(title)} (${shortLink})`}
- />
- <meta name="twitter:card" content="summary_large_image" />
- <meta name="twitter:site" content={unescape(fullDomain)} />
- <meta name="twitter:title" content={unescape(title)} />
- <meta name="twitter:description" content={unescape(description)} />
- <meta name="twitter:image" content={unescape(image)} />
- <meta
- property="twitter:image:alt"
- content={`OG image for ${unescape(title)} (${shortLink})`}
- />
- <meta charSet="utf-8" />
- <link
- rel="icon"
- href={`${GOOGLE_FAVICON_URL}${unescape(apexDomain)}`}
- />
- </Head>
- <main className="flex h-screen w-screen items-center justify-center">
- <div className="mx-5 w-full max-w-lg overflow-hidden rounded-lg border border-gray-200 sm:mx-0">
- <img src={image} alt={title} className="w-full object-cover" />
- <div className="flex space-x-3 bg-gray-100 p-5">
- <img
- src={`${GOOGLE_FAVICON_URL}${unescape(apexDomain)}`}
- alt={title}
- className="mt-1 h-6 w-6"
- />
- <div className="flex flex-col space-y-3">
- <h1 className="font-bold text-gray-700">{title}</h1>
- <p className="text-sm text-gray-500">{description}</p>
- </div>
- </div>
- </div>
- </main>
- </>
- );
-}
-
-export async function getServerSideProps(ctx) {
- const { domain, key } = ctx.params as { domain: string; key: string };
- const link = await prisma.link.findUnique({
- where: {
- domain_key: {
- domain,
- key,
- },
- },
- });
-
- const { url, proxy, title, description, image } = link || {};
-
- if (!url) {
- return {
- notFound: true,
- };
- // if it's not a proxy page, redirect to the original URL
- } else if (!proxy) {
- return {
- redirect: {
- destination: url,
- },
- };
- }
-
- const fullDomain = new URL(url).hostname;
- const apexDomain = getApexDomain(url);
-
- return {
- props: {
- shortLink: `${domain}/${key}`,
- fullDomain,
- apexDomain,
- title,
- description,
- image,
- },
- };
-}