The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Added on-demand ISR for proxy page + favicon implementation
Steven Tey77440a565969
diff --git a/lib/middleware/link.ts b/lib/middleware/link.tsindex 807ff63977..b15b3c2015 100644--- a/lib/middleware/link.ts+++ b/lib/middleware/link.ts@@ -42,6 +42,7 @@ export default async function LinkMiddleware(ev.waitUntil(recordClick(hostname, req, key)); // increment click countif (image && description && isBot) {+ // rewrite to proxy page (dub.sh/proxy/[domain]/[key])res = NextResponse.rewrite(`https://dub.sh/proxy/${hostname}/${key}`);} else {res = NextResponse.redirect(target);diff --git a/pages/api/projects/[slug]/domains/[domain]/links/[key]/index.ts b/pages/api/projects/[slug]/domains/[domain]/links/[key]/index.tsindex b9ad8e1b95..b2c1850e4e 100644--- a/pages/api/projects/[slug]/domains/[domain]/links/[key]/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/links/[key]/index.ts@@ -5,11 +5,17 @@ import cloudinary from "cloudinary";export default withProjectAuth(async (req: NextApiRequest, res: NextApiResponse) => {- const { domain, key: oldKey } = req.query as {+ const {+ slug,+ domain,+ key: oldKey,+ } = req.query as {+ slug: string;domain: string;key: string;};+ // PUT /api/projects/[slug]/domains/[domain]/links/[key] - edit linkif (req.method === "PUT") {let { key, url, title, timestamp, description, image } = req.body;if (!key || !url || !title || !timestamp) {@@ -30,6 +36,16 @@ export default withProjectAuth(invalidate: true,});}+ // if there's an image, revalidate proxy page (dub.sh/proxy/[domain]/[key])+ await fetch(+ `https://dub.sh/api/projects/${slug}/domains/${domain}/links/${oldKey}/revalidate`,+ {+ method: "PUT",+ headers: {+ "Content-Type": "application/json",+ },+ }+ );}const response = await editLink(domain, oldKey, {key,diff --git a/pages/api/projects/[slug]/domains/[domain]/links/[key]/revalidate.ts b/pages/api/projects/[slug]/domains/[domain]/links/[key]/revalidate.tsnew file mode 100644index 0000000000..77225adec4--- /dev/null+++ b/pages/api/projects/[slug]/domains/[domain]/links/[key]/revalidate.ts@@ -0,0 +1,22 @@+import type { NextApiRequest, NextApiResponse } from "next";++export default async function handler(+ req: NextApiRequest,+ res: NextApiResponse+) {+ const { domain, key } = req.query as { domain: string; key: string };++ res.setHeader("Access-Control-Allow-Origin", "https://app.dub.sh");+ res.setHeader("Access-Control-Allow-Methods", "PUT");++ try {+ const response = await res.revalidate(`/proxy/${domain}/${key}`);+ res.status(200).json({+ message: "OK",+ });+ } catch (error) {+ res.status(500).json({+ message: `Failed to revalidate "${`/proxy/${domain}/${key}`}"`,+ });+ }+}diff --git a/pages/proxy/[domain]/[key].tsx b/pages/proxy/[domain]/[key].tsxindex 54580203b9..13bd85b334 100644--- a/pages/proxy/[domain]/[key].tsx+++ b/pages/proxy/[domain]/[key].tsx@@ -1,10 +1,10 @@-export const config = { runtime: "experimental-edge" };import { redis } from "@/lib/upstash";+import prisma from "@/lib/prisma";import { LinkProps } from "@/lib/types";import Head from "next/head";import { escape } from "html-escaper";-export default function LinkPage({ url, title, description, image }) {+export default function LinkPage({ url, title, description, image, logo }) {return (<Head><meta property="og:title" content={escape(title)} />@@ -16,11 +16,19 @@ export default function LinkPage({ url, title, description, image }) {<meta name="twitter:title" content={escape(title)} /><meta name="twitter:description" content={escape(description)} /><meta name="twitter:image" content={escape(image)} />+ {logo && <link rel="icon" href={logo} />}</Head>);}-export async function getServerSideProps(ctx) {+export function getStaticPaths() {+ return {+ paths: [],+ fallback: "blocking",+ };+}++export async function getStaticProps(ctx) {const { domain, key } = ctx.params;const response = await redis.hget<Omit<LinkProps, "key">>(`${domain}:links`,@@ -32,6 +40,14 @@ export async function getServerSideProps(ctx) {notFound: true,};}+ const project = await prisma.project.findUnique({+ where: {+ domain,+ },+ select: {+ logo: true,+ },+ });const hostname = new URL(url).hostname;@@ -41,6 +57,7 @@ export async function getServerSideProps(ctx) {title,description,image,+ logo: project?.logo,},};}diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex b237110d8c..e030f464d8 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -64,6 +64,7 @@ model Project {slug String @uniquedomain String @uniquedomainVerified Boolean @default(false)+ logo String?plan String @default("free")usageLimit Int @default(1000)stripeId String? @unique // Stripe customer ID