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.

Fixed link cloaking

Steven Teya2e657776172
diff --git a/app/proxy/[domain]/[key]/page.tsx b/app/proxy/[domain]/[key]/page.tsx
index 5c7c864815..2fae58dc65 100644
--- a/app/proxy/[domain]/[key]/page.tsx
+++ b/app/proxy/[domain]/[key]/page.tsx
@@ -31,7 +31,7 @@ export async function generateMetadata({
});
}
-export default async function ProxyOGPage({
+export default async function ProxyPage({
params,
}: {
params: { domain: string; key: string };
diff --git a/app/rewrite/[url]/page.tsx b/app/rewrite/[url]/page.tsx
new file mode 100644
index 0000000000..94c3149e89
--- /dev/null
+++ b/app/rewrite/[url]/page.tsx
@@ -0,0 +1,35 @@
+import { constructMetadata, getApexDomain } from "#/lib/utils";
+import { GOOGLE_FAVICON_URL } from "#/lib/constants";
+import { getMetaTags } from "@/pages/api/edge/metatags";
+
+export const runtime = "edge";
+
+export async function generateMetadata({
+ params,
+}: {
+ params: { url: string };
+}) {
+ const url = decodeURIComponent(params.url); // key can potentially be encoded
+
+ const metatags = await getMetaTags(url);
+
+ const apexDomain = getApexDomain(url);
+
+ return constructMetadata({
+ title: metatags.title,
+ description: metatags.description,
+ image: metatags.image || undefined,
+ icons: `${GOOGLE_FAVICON_URL}${apexDomain}`,
+ noIndex: true,
+ });
+}
+
+export default async function RewritePage({
+ params,
+}: {
+ params: { url: string };
+}) {
+ const url = decodeURIComponent(params.url);
+
+ return <iframe src={url} className="min-h-screen w-full border-none" />;
+}
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index 3e1c12695b..6c9258961b 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -91,7 +91,10 @@ export default async function LinkMiddleware(
// rewrite to target URL if link cloaking is enabled
if (rewrite) {
- return NextResponse.rewrite(decodeURIComponent(target), DUB_HEADERS);
+ return NextResponse.rewrite(
+ new URL(`/rewrite/${target}`, req.url),
+ DUB_HEADERS,
+ );
// rewrite to proxy page (/_proxy/[domain]/[key]) if it's a bot and proxy is enabled
} else if (isBot && proxy) {
diff --git a/pages/api/edge/metatags.ts b/pages/api/edge/metatags.ts
index d75cdb7428..0ed35344c8 100644
--- a/pages/api/edge/metatags.ts
+++ b/pages/api/edge/metatags.ts
@@ -108,7 +108,7 @@ const getRelativeUrl = (url: string, imageUrl: string) => {
return new URL(imageUrl, baseURL).toString();
};
-export const getMetaTags = async (url: string, ev: NextFetchEvent) => {
+export const getMetaTags = async (url: string, ev?: NextFetchEvent) => {
const html = await getHtml(url);
if (!html) {
return {
@@ -147,7 +147,7 @@ export const getMetaTags = async (url: string, ev: NextFetchEvent) => {
object["icon"] ||
object["shortcut icon"];
- ev.waitUntil(
+ ev?.waitUntil(
recordMetatags(url, title && description && image ? false : true),
);