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.

Implement multi-site setup

Steven Tey5bb4535d45e7
diff --git a/app/(marketing)/layout.tsx b/app/(marketing)/layout.tsx
deleted file mode 100644
index 88d370e4e4..0000000000
--- a/app/(marketing)/layout.tsx
+++ /dev/null
@@ -1,35 +0,0 @@
-import Link from "next/link";
-import { Github, Logo, Twitter } from "@/components/shared/icons";
-import Nav from "#/ui/home/nav";
-import { headers } from "next/headers";
-import { getDomain } from "@/lib/utils";
-
-export default function MarketingLayout(props) {
- const domain = getDomain(headers());
- return (
- <div className="flex min-h-screen flex-col justify-between">
- {/* Only show stats modal if not on the /stats page */}
- {props.children.props.childProp.segment !== "stats" && props.modal}
- <Nav domain={domain} />
- {props.children}
- <div className="z-10 flex h-20 items-center justify-center space-x-12 border-t border-gray-200">
- <a href="https://twitter.com/dubdotsh" target="_blank" rel="noreferrer">
- <span className="sr-only">Twitter</span>
- <Twitter className="h-6 w-6 text-gray-600" />
- </a>
- <Link href={domain === "dub.sh" ? "/" : `https://dub.sh`}>
- <span className="sr-only">Dub.sh Logo</span>
- <Logo className="h-7 w-7 text-gray-600" />
- </Link>
- <a
- href="https://github.com/steven-tey/dub"
- target="_blank"
- rel="noreferrer"
- >
- <span className="sr-only">Github</span>
- <Github className="h-6 w-6 text-gray-600" />
- </a>
- </div>
- </div>
- );
-}
diff --git a/app/(marketing)/stats/[key]/logo.png b/app/(marketing)/stats/[key]/logo.png
deleted file mode 100644
index 1653c4e3cd..0000000000
Binary files a/app/(marketing)/stats/[key]/logo.png and /dev/null differ
diff --git a/app/[domain]/layout.tsx b/app/[domain]/layout.tsx
new file mode 100644
index 0000000000..75e32bb028
--- /dev/null
+++ b/app/[domain]/layout.tsx
@@ -0,0 +1,12 @@
+import Nav from "#/ui/home/nav";
+import Footer from "#/ui/home/footer";
+
+export default function CustomDomainLayout(props) {
+ return (
+ <div className="flex min-h-screen flex-col justify-between">
+ <Nav />
+ {props.children}
+ <Footer />
+ </div>
+ );
+}
diff --git a/app/[domain]/page.tsx b/app/[domain]/page.tsx
new file mode 100644
index 0000000000..ea94f8558b
--- /dev/null
+++ b/app/[domain]/page.tsx
@@ -0,0 +1,44 @@
+import prisma from "@/lib/prisma";
+import PlaceholderContent from "./placeholder";
+
+export async function generateMetadata({
+ params,
+}: {
+ params: { domain: string };
+}) {
+ const title = `${params.domain.toUpperCase()} - A Dub.sh Custom Domain`;
+ const description = `${params.domain.toUpperCase()} is a custom domain on Dub - an open-source link management tool for modern marketing teams to create, share, and track short links.`;
+
+ return {
+ title,
+ description,
+ twitter: {
+ card: "summary_large_image",
+ title,
+ description,
+ creator: "@dubdotsh",
+ },
+ };
+}
+
+export async function generateStaticParams() {
+ const domains =
+ process.env.VERCEL_ENV === "production"
+ ? await prisma.domain.findMany({
+ where: {
+ verified: true,
+ target: null,
+ },
+ select: {
+ slug: true,
+ },
+ })
+ : [];
+ return domains.map(({ slug: domain }) => ({
+ domain,
+ }));
+}
+
+export default function CustomDomainPage() {
+ return <PlaceholderContent />;
+}
diff --git a/pages/_root/[domain].tsx b/app/[domain]/placeholder.tsx
similarity index 76%
rename from pages/_root/[domain].tsx
rename to app/[domain]/placeholder.tsx
index d6cb0a65a6..a8f7dbf050 100644
--- a/pages/_root/[domain].tsx
+++ b/app/[domain]/placeholder.tsx
@@ -1,14 +1,16 @@
-import prisma from "@/lib/prisma";
+"use client";
+
import Background from "@/components/shared/background";
-import Meta from "@/components/layout/meta";
import { useState } from "react";
import { motion } from "framer-motion";
import { useDebounce } from "use-debounce";
import { STAGGER_CHILD_VARIANTS } from "@/lib/constants";
import Spline from "@splinetool/react-spline";
import { InlineSnippet } from "@/components/app/domains/domain-configuration";
+import { useParams } from "next/navigation";
-export default function Placeholder({ domain }: { domain: string }) {
+export default function PlaceholderContent() {
+ const { domain } = useParams() as { domain: string };
const [loading, setLoading] = useState(true);
const onLoad = () => {
setLoading(false);
@@ -20,10 +22,6 @@ export default function Placeholder({ domain }: { domain: string }) {
return (
<div className="flex h-screen flex-col items-center">
- <Meta
- title={`${domain.toUpperCase()} - A Dub Custom Domain`}
- description={`${domain.toUpperCase()} is a custom domain on Dub - an open-source link management tool for modern marketing teams to create, share, and track short links.`}
- />
<Background />
<motion.div
className="z-10"
@@ -87,33 +85,3 @@ export default function Placeholder({ domain }: { domain: string }) {
</div>
);
}
-
-export const getStaticPaths = async () => {
- const domains = process.env.VERCEL_ENV === "production" ? await prisma.domain.findMany({
- where: {
- verified: true,
- target: null,
- },
- select: {
- slug: true,
- },
- }) : []
-
- return {
- paths: domains.map(({ slug: domain }) => ({
- params: {
- domain,
- },
- })),
- fallback: "blocking",
- };
-};
-
-export const getStaticProps = async (context) => {
- const { domain } = context.params;
- return {
- props: {
- domain,
- },
- };
-};
diff --git a/app/[domain]/stats/[key]/page.tsx b/app/[domain]/stats/[key]/page.tsx
new file mode 100644
index 0000000000..72ea1715b5
--- /dev/null
+++ b/app/[domain]/stats/[key]/page.tsx
@@ -0,0 +1,52 @@
+import { notFound } from "next/navigation";
+import { getLinkViaEdge } from "@/lib/planetscale";
+import Stats from "#/ui/stats";
+
+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.publicStats) {
+ return;
+ }
+
+ const title = `Stats for ${params.domain}/${params.key} - Dub`;
+ const description = `Stats page for ${params.domain}/${params.key}, which redirects to ${data.url}.`;
+ const image = `https://${params.domain}/api/og/stats?domain=${params.domain}&key=${params.key}`;
+
+ return {
+ title,
+ description,
+ image,
+ twitter: {
+ card: "summary_large_image",
+ title,
+ description,
+ image,
+ creator: "@dubdotsh",
+ },
+ };
+}
+
+export default async function StatsPage({
+ params,
+}: {
+ params: { domain: string; key: string };
+}) {
+ const data = await getLinkViaEdge(params.domain, params.key);
+
+ if (!data || !data.publicStats) {
+ notFound();
+ }
+
+ return (
+ <div className="bg-gray-50">
+ <Stats staticDomain={params.domain} />
+ </div>
+ );
+}
diff --git a/app/(marketing)/@modal/(...)stats/[key]/default.tsx b/app/dub.sh/@modal/(...)stats/[key]/default.tsx
similarity index 100%
rename from app/(marketing)/@modal/(...)stats/[key]/default.tsx
rename to app/dub.sh/@modal/(...)stats/[key]/default.tsx
diff --git a/app/(marketing)/@modal/(...)stats/[key]/page.tsx b/app/dub.sh/@modal/(...)stats/[key]/page.tsx
similarity index 100%
rename from app/(marketing)/@modal/(...)stats/[key]/page.tsx
rename to app/dub.sh/@modal/(...)stats/[key]/page.tsx
diff --git a/app/(marketing)/@modal/default.tsx b/app/dub.sh/@modal/default.tsx
similarity index 100%
rename from app/(marketing)/@modal/default.tsx
rename to app/dub.sh/@modal/default.tsx
diff --git a/app/atom/route.ts b/app/dub.sh/atom/route.ts
similarity index 100%
rename from app/atom/route.ts
rename to app/dub.sh/atom/route.ts
diff --git a/app/dub.sh/changelog/[slug]/page.tsx b/app/dub.sh/changelog/[slug]/page.tsx
new file mode 100644
index 0000000000..45b4a5d18e
--- /dev/null
+++ b/app/dub.sh/changelog/[slug]/page.tsx
@@ -0,0 +1,155 @@
+import type { Metadata } from "next";
+import { notFound } from "next/navigation";
+import { allChangelogPosts } from "contentlayer/generated";
+import { MDX } from "#/ui/blog/mdx";
+import Link from "next/link";
+import { formatDate } from "@/lib/utils";
+import { getBlurDataURL } from "@/lib/images";
+import BlurImage from "#/ui/blur-image";
+import Author from "#/ui/blog/author";
+import { Facebook, LinkedIn, Twitter } from "@/components/shared/icons";
+
+export async function generateStaticParams() {
+ return allChangelogPosts.map((post) => ({
+ slug: post.slug,
+ }));
+}
+
+export async function generateMetadata({
+ params,
+}: {
+ params: { slug: string };
+}): Promise<Metadata | undefined> {
+ const post = allChangelogPosts.find((post) => post.slug === params.slug);
+ if (!post) {
+ return;
+ }
+
+ const {
+ title,
+ publishedAt: publishedTime,
+ summary: description,
+ image,
+ slug,
+ } = post;
+
+ return {
+ title: `${title} - Dub Changelog`,
+ description,
+ openGraph: {
+ title: `${title} - Dub Changelog`,
+ description,
+ type: "article",
+ publishedTime,
+ url: `https://dub.sh/changelog/${slug}`,
+ images: [
+ {
+ url: image,
+ },
+ ],
+ },
+ twitter: {
+ card: "summary_large_image",
+ title,
+ description,
+ images: [image],
+ },
+ };
+}
+
+export default async function ChangelogPost({
+ params,
+}: {
+ params: { slug: string };
+}) {
+ const post = allChangelogPosts.find((post) => post.slug === params.slug);
+ if (!post) {
+ notFound();
+ }
+
+ return (
+ <div className="mx-auto my-20 grid max-w-screen-xl md:grid-cols-4 md:px-20">
+ <div className="sticky top-10 hidden self-start md:col-span-1 md:block">
+ <Link
+ href="/changelog"
+ className="text-sm text-gray-500 transition-colors hover:text-gray-800"
+ >
+ ← Back to Changelog
+ </Link>
+ </div>
+ <div className="flex flex-col space-y-8 md:col-span-3">
+ <div className="mx-5 grid gap-5 md:mx-0">
+ <div className="flex flex-col">
+ <Link
+ href="/changelog"
+ className="my-5 text-sm text-gray-500 md:hidden"
+ >
+ ← Back to Changelog
+ </Link>
+ <time
+ dateTime={post.publishedAt}
+ className="flex items-center text-sm text-gray-500 md:text-base"
+ >
+ {formatDate(post.publishedAt)}
+ </time>
+ </div>
+ <h1 className="font-display text-3xl font-bold tracking-tight text-gray-800 sm:text-4xl">
+ {post.title}
+ </h1>
+ </div>
+ <BlurImage
+ src={post.image}
+ alt={post.title}
+ width={1200}
+ height={900}
+ priority // since it's above the fold
+ placeholder="blur"
+ blurDataURL={await getBlurDataURL(post.image!)}
+ className="border border-gray-100 md:rounded-2xl"
+ />
+ <div className="mx-5 mb-10 flex items-center justify-between md:mx-0">
+ {/* @ts-expect-error Async Server Component */}
+ <Author username={post.author} />
+ <div className="flex items-center space-x-6">
+ <Link
+ href={`https://twitter.com/intent/tweet?text=${post.title}&url=https://dub.sh/changelog/${post.slug}&via=${post.author}`}
+ target="_blank"
+ rel="noopener noreferrer"
+ className="transition-all hover:scale-110"
+ >
+ <Twitter className="h-6 w-6" />
+ </Link>
+ <Link
+ href={`
+ http://www.linkedin.com/shareArticle?mini=true&url=https://dub.sh/changelog/${post.slug}`}
+ target="_blank"
+ rel="noopener noreferrer"
+ className="transition-all hover:scale-110"
+ >
+ <LinkedIn className="h-6 w-6" fill="black" />
+ </Link>
+ <Link
+ href={`https://www.facebook.com/sharer/sharer.php?u=https://dub.sh/changelog/${post.slug}`}
+ target="_blank"
+ rel="noopener noreferrer"
+ className="transition-all hover:scale-110"
+ >
+ <Facebook className="h-6 w-6" fill="black" />
+ </Link>
+ </div>
+ </div>
+ <MDX code={post.body.code} />
+ <div className="mt-10 flex justify-end border-t border-gray-200 pt-5">
+ <Link
+ href={`https://github.com/steven-tey/dub/blob/main/posts/changelog/${params.slug}.mdx`}
+ target="_blank"
+ rel="noopener noreferrer"
+ className="text-sm text-gray-500 transition-colors hover:text-gray-800"
+ >
+ <p>Found a typo? Edit this page →</p>
+ </Link>
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/app/(marketing)/changelog/opengraph-image.png b/app/dub.sh/changelog/opengraph-image.png
similarity index 100%
rename from app/(marketing)/changelog/opengraph-image.png
rename to app/dub.sh/changelog/opengraph-image.png
diff --git a/app/dub.sh/changelog/page.tsx b/app/dub.sh/changelog/page.tsx
new file mode 100644
index 0000000000..71e5f4f4c6
--- /dev/null
+++ b/app/dub.sh/changelog/page.tsx
@@ -0,0 +1,114 @@
+import BlurImage from "#/ui/blur-image";
+import { getBlurDataURL } from "@/lib/images";
+import { allChangelogPosts } from "contentlayer/generated";
+import { Metadata } from "next";
+import { formatDate } from "@/lib/utils";
+import Link from "next/link";
+import { MDX } from "#/ui/blog/mdx";
+import { Twitter } from "@/components/shared/icons";
+import { Rss } from "lucide-react";
+
+export const metadata: Metadata = {
+ title: "Changelog - Dub",
+ description:
+ "All the latest updates, improvements, and fixes to Dub - the link management tool for modern marketing teams.",
+};
+
+export default async function Changelog() {
+ return (
+ <div className="mx-auto max-w-screen-xl md:px-20">
+ <div className="relative grid border-b border-gray-200 py-20 md:grid-cols-4">
+ <div className="md:col-span-1" />
+ <div className="mx-5 flex flex-col space-y-6 md:col-span-3 md:mx-0">
+ <h1 className="font-display text-4xl font-bold tracking-tight text-gray-800 md:text-5xl">
+ Changelog
+ </h1>
+ <p className="text-lg text-gray-500">
+ All the latest updates, improvements, and fixes to Dub.
+ </p>
+ </div>
+ <div className="absolute bottom-2 right-0 flex items-center space-x-2">
+ <p className="text-sm text-gray-500">Subscribe to updates →</p>
+ <Link
+ href="https://twitter.com/dubdotsh"
+ className="rounded-full bg-blue-100 p-2 transition-colors hover:bg-blue-200"
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ <Twitter className="h-4 w-4 text-[#1d9bf0]" />
+ </Link>
+ <Link
+ href="/atom"
+ className="rounded-full bg-gray-100 p-2 transition-colors hover:bg-gray-200"
+ >
+ <Rss className="h-4 w-4 text-gray-500" />
+ </Link>
+ </div>
+ </div>
+
+ <div className="divide-y divide-gray-200">
+ {/* @ts-expect-error Async Server Component */}
+ {allChangelogPosts
+ .sort((a, b) => {
+ if (new Date(a.publishedAt) > new Date(b.publishedAt)) {
+ return -1;
+ }
+ return 1;
+ })
+ .map(async (post, idx) => (
+ <div
+ key={idx}
+ className="grid py-20 md:grid-cols-4 md:px-5 xl:px-0"
+ >
+ <div className="sticky top-10 hidden self-start md:col-span-1 md:block">
+ <Link href={`/changelog/${post.slug}`}>
+ <time
+ dateTime={post.publishedAt}
+ className="text-gray-500 transition-colors hover:text-gray-800"
+ >
+ {formatDate(post.publishedAt)}
+ </time>
+ </Link>
+ </div>
+ <div className="md:col-span-3">
+ <div className="flex flex-col gap-6">
+ <Link href={`/changelog/${post.slug}`}>
+ <BlurImage
+ src={post.image}
+ alt={post.title}
+ width={1200}
+ height={900}
+ priority={idx === 0} // since it's above the fold
+ placeholder="blur"
+ blurDataURL={await getBlurDataURL(post.image!)}
+ className="border border-gray-100 md:rounded-2xl"
+ />
+ </Link>
+ <Link
+ href={`/changelog/${post.slug}`}
+ className="group mx-5 flex items-center space-x-3 md:mx-0"
+ >
+ <time
+ dateTime={post.publishedAt}
+ className="text-sm text-gray-500 transition-all group-hover:text-gray-800 md:hidden"
+ >
+ {formatDate(post.publishedAt)}
+ </time>
+ </Link>
+ <Link
+ href={`/changelog/${post.slug}`}
+ className="mx-5 md:mx-0"
+ >
+ <h2 className="font-display text-3xl font-bold tracking-tight text-gray-800 hover:underline hover:decoration-1 hover:underline-offset-4 md:text-4xl">
+ {post.title}
+ </h2>
+ </Link>
+ <MDX code={post.body.code} />
+ </div>
+ </div>
+ </div>
+ ))}
+ </div>
+ </div>
+ );
+}
diff --git a/app/default.tsx b/app/dub.sh/default.tsx
similarity index 100%
rename from app/default.tsx
rename to app/dub.sh/default.tsx
diff --git a/app/dub.sh/layout.tsx b/app/dub.sh/layout.tsx
new file mode 100644
index 0000000000..38a5f67095
--- /dev/null
+++ b/app/dub.sh/layout.tsx
@@ -0,0 +1,14 @@
+import Nav from "#/ui/home/nav";
+import Footer from "#/ui/home/footer";
+
+export default function MarketingLayout(props) {
+ return (
+ <div className="flex min-h-screen flex-col justify-between">
+ {/* Only show stats modal if not on the /stats page */}
+ {props.children.props.childProp.segment !== "stats" && props.modal}
+ <Nav />
+ {props.children}
+ <Footer />
+ </div>
+ );
+}
diff --git a/app/(marketing)/metatags/content.tsx b/app/dub.sh/metatags/content.tsx
similarity index 100%
rename from app/(marketing)/metatags/content.tsx
rename to app/dub.sh/metatags/content.tsx
diff --git a/app/(marketing)/metatags/launch.tsx b/app/dub.sh/metatags/launch.tsx
similarity index 100%
rename from app/(marketing)/metatags/launch.tsx
rename to app/dub.sh/metatags/launch.tsx
diff --git a/app/(marketing)/metatags/page.tsx b/app/dub.sh/metatags/page.tsx
similarity index 100%
rename from app/(marketing)/metatags/page.tsx
rename to app/dub.sh/metatags/page.tsx
diff --git a/app/(marketing)/page.tsx b/app/dub.sh/page.tsx
similarity index 100%
rename from app/(marketing)/page.tsx
rename to app/dub.sh/page.tsx
diff --git a/app/(marketing)/stats/[key]/page.tsx b/app/dub.sh/stats/[key]/page.tsx
similarity index 76%
rename from app/(marketing)/stats/[key]/page.tsx
rename to app/dub.sh/stats/[key]/page.tsx
index f196dd9e56..40ab81d095 100644
--- a/app/(marketing)/stats/[key]/page.tsx
+++ b/app/dub.sh/stats/[key]/page.tsx
@@ -11,24 +11,25 @@ export async function generateMetadata({
}: {
params: { key: string };
}) {
- const domain = getDomain(headers());
-
- const data = await getLinkViaEdge(domain, params.key);
+ const data = await getLinkViaEdge("dub.sh", params.key);
if (!data || !data.publicStats) {
return;
}
- const title = `Stats for ${domain}/${params.key} - Dub`;
- const description = `Stats page for ${domain}/${params.key}, which redirects to ${data.url}.`;
+ const title = `Stats for dub.sh/${params.key} - Dub`;
+ const description = `Stats page for dub.sh/${params.key}, which redirects to ${data.url}.`;
+ const image = `https://dub.sh/api/og/stats?domain=dub.sh&key=${params.key}`;
return {
title,
description,
+ image,
twitter: {
card: "summary_large_image",
title,
description,
+ image,
creator: "@dubdotsh",
},
};
diff --git a/app/ui/home/footer.tsx b/app/ui/home/footer.tsx
new file mode 100644
index 0000000000..5af0d100d5
--- /dev/null
+++ b/app/ui/home/footer.tsx
@@ -0,0 +1,30 @@
+"use client";
+
+import Link from "next/link";
+import { Github, Logo, Twitter } from "@/components/shared/icons";
+import { useParams } from "next/navigation";
+
+export default function Footer() {
+ const { domain = "dub.sh" } = useParams() as { domain: string };
+
+ return (
+ <div className="z-10 flex h-20 items-center justify-center space-x-12 border-t border-gray-200">
+ <a href="https://twitter.com/dubdotsh" target="_blank" rel="noreferrer">
+ <span className="sr-only">Twitter</span>
+ <Twitter className="h-6 w-6 text-gray-600" />
+ </a>
+ <Link href={domain === "dub.sh" ? "/" : `https://dub.sh`}>
+ <span className="sr-only">Dub.sh Logo</span>
+ <Logo className="h-7 w-7 text-gray-600" />
+ </Link>
+ <a
+ href="https://github.com/steven-tey/dub"
+ target="_blank"
+ rel="noreferrer"
+ >
+ <span className="sr-only">Github</span>
+ <Github className="h-6 w-6 text-gray-600" />
+ </a>
+ </div>
+ );
+}
diff --git a/app/ui/home/nav.tsx b/app/ui/home/nav.tsx
index cbfa38aa45..394fb69dad 100644
--- a/app/ui/home/nav.tsx
+++ b/app/ui/home/nav.tsx
@@ -2,13 +2,15 @@
import Link from "next/link";
import Image from "next/image";
-import { useSelectedLayoutSegment } from "next/navigation";
+import { useParams, useSelectedLayoutSegment } from "next/navigation";
import useScroll from "#/lib/hooks/use-scroll";
import clsx from "clsx";
const transparentHeaderSegments = new Set(["metatags"]);
-export default function Nav({ domain }: { domain?: string }) {
+export default function Nav() {
+ const { domain = "dub.sh" } = useParams() as { domain: string };
+
const scrolled = useScroll(80);
const segment = useSelectedLayoutSegment();
diff --git a/lib/middleware/root.ts b/lib/middleware/root.ts
index 02c922ae35..4ebbb0048c 100644
--- a/lib/middleware/root.ts
+++ b/lib/middleware/root.ts
@@ -17,7 +17,7 @@ export default async function RootMiddleware(
}
if (isHomeHostname(domain)) {
- return NextResponse.next();
+ return NextResponse.rewrite(new URL(`/dub.sh`, req.url));
} else {
ev.waitUntil(recordClick(domain, req)); // record clicks on root page (if domain is not dub.sh)
@@ -31,7 +31,7 @@ export default async function RootMiddleware(
}
} else {
// rewrite to root page unless the user defines a site to redirect to
- return NextResponse.rewrite(new URL(`/_root/${domain}`, req.url));
+ return NextResponse.rewrite(new URL(`/${domain}`, req.url));
}
}
}
diff --git a/middleware.ts b/middleware.ts
index 7d01427006..586bfe87b8 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -15,12 +15,12 @@ export const config = {
* Match all paths except for:
* 1. /api/ routes
* 2. /_next/ (Next.js internals)
- * 3. /_proxy/, /_auth/, /_root/ (special pages for OG tags proxying, password protection, and placeholder _root pages)
+ * 3. /_proxy/, /_auth/ (special pages for OG tags proxying and password protection)
* 4. /_static (inside /public)
* 5. /_vercel (Vercel internals)
* 6. all root files inside /public (e.g. /favicon.ico)
*/
- "/((?!api/|_next/|_proxy/|_auth/|_root/|_static|_vercel|[\\w-]+\\.\\w+).*)",
+ "/((?!api/|_next/|_proxy/|_auth/|_static|_vercel|favicon.ico).*)",
],
};
@@ -40,7 +40,7 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
// for public stats pages (e.g. dub.sh/stats/github)
if (path.startsWith("/stats/")) {
- return NextResponse.next();
+ return NextResponse.rewrite(new URL(`/${domain}${path}`, req.url));
}
// for root pages (e.g. dub.sh, vercel.fyi, etc.)
@@ -53,7 +53,7 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
return NextResponse.redirect(DEFAULT_REDIRECTS[key]);
}
if (await isReservedKey(key)) {
- return NextResponse.next();
+ return NextResponse.rewrite(new URL(`/dub.sh${path}`, req.url));
}
}
diff --git a/app/(marketing)/stats/[key]/opengraph-image.tsx b/pages/api/og/stats.tsx
similarity index 84%
rename from app/(marketing)/stats/[key]/opengraph-image.tsx
rename to pages/api/og/stats.tsx
index 8af3035501..37b7016ebc 100644
--- a/app/(marketing)/stats/[key]/opengraph-image.tsx
+++ b/pages/api/og/stats.tsx
@@ -1,8 +1,7 @@
-import { ImageResponse } from "next/server";
-import { headers } from "next/headers";
+import { ImageResponse, NextRequest } from "next/server";
import { getLinkViaEdge } from "@/lib/planetscale";
import { getStats } from "@/lib/stats";
-import { getDomain, nFormatter, truncate } from "@/lib/utils";
+import { nFormatter, truncate } from "@/lib/utils";
export const runtime = "edge";
export const contentType = "image/png";
@@ -15,15 +14,16 @@ const satoshiBold = fetch(
new URL("@/styles/Satoshi-Bold.ttf", import.meta.url),
).then((res) => res.arrayBuffer());
-export default async function StatsOG({ params }: { params: { key: string } }) {
+export default async function handler(req: NextRequest) {
const [satoshiBlackData, satoshiBoldData] = await Promise.all([
satoshiBLack,
satoshiBold,
]);
- const domain = getDomain(headers());
+ const domain = req.nextUrl.searchParams.get("domain") || "dub.sh";
+ const key = req.nextUrl.searchParams.get("key") || "github";
- const data = await getLinkViaEdge(domain, params.key);
+ const data = await getLinkViaEdge(domain, key);
if (!data?.publicStats) {
return new Response(`Stats for this link are not public`, {
status: 403,
@@ -32,7 +32,7 @@ export default async function StatsOG({ params }: { params: { key: string } }) {
const timeseries = await getStats({
domain,
- key: params.key,
+ key,
endpoint: "timeseries",
interval: "30d",
});
@@ -51,13 +51,16 @@ export default async function StatsOG({ params }: { params: { key: string } }) {
alignItems: "center",
backgroundColor: "white",
backgroundImage: `url(${new URL(
- "./background.png",
+ "../../../public/_static/background.png",
import.meta.url,
).toString()})`,
}}
>
<img
- src={new URL("./logo.png", import.meta.url).toString()}
+ src={new URL(
+ "../../../public/_static/logo.png",
+ import.meta.url,
+ ).toString()}
style={{
width: "80px",
height: "80px",
@@ -78,7 +81,7 @@ export default async function StatsOG({ params }: { params: { key: string } }) {
lineHeight: "7rem",
}}
>
- {domain}/{truncate(params.key, 12)}
+ {domain}/{truncate(key, 12)}
</h1>
<p
style={{
diff --git a/app/(marketing)/stats/[key]/background.png b/public/_static/background.png
similarity index 100%
rename from app/(marketing)/stats/[key]/background.png
rename to public/_static/background.png