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.

Debug Rewrites

Steven Teyd6ffc0e5fbfa
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index 192a8a22e6..63133f3eeb 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -81,12 +81,16 @@ export default async function LinkMiddleware(
);
}
+ const isBot = detectBot(req);
+
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
+ // rewrite to target URL if link cloaking is enabled
+ if (rewrite) {
+ return NextResponse.rewrite(new URL(target, req.url), DUB_HEADERS);
+ } else if (isBot && proxy) {
+ // rewrite to proxy page (/_proxy/[domain]/[key]) if it's a bot and proxy is enabled
return NextResponse.rewrite(
new URL(`/proxy/${domain}/${encodeURIComponent(key)}`, req.url),
);
@@ -103,10 +107,8 @@ export default async function LinkMiddleware(
DUB_HEADERS,
);
} else {
- // regular redirect / rewrite
- return rewrite
- ? NextResponse.rewrite(getFinalUrl(target, { req }), DUB_HEADERS)
- : NextResponse.redirect(getFinalUrl(target, { req }), DUB_HEADERS);
+ // regular redirect
+ return NextResponse.redirect(getFinalUrl(target, { req }), DUB_HEADERS);
}
} else {
// short link not found, redirect to root
diff --git a/lib/middleware/utils.ts b/lib/middleware/utils.ts
index a3b67a9641..8169bd9ebe 100644
--- a/lib/middleware/utils.ts
+++ b/lib/middleware/utils.ts
@@ -3,7 +3,10 @@ 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
+ if (domain === "dub.localhost:8888" || domain === "staging.dub.sh") {
+ // for local development & staging environments
+ domain = "dub.sh";
+ }
// path is the path of the URL (e.g. dub.co/stats/github -> /stats/github)
const path = req.nextUrl.pathname;