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.

Support passthrough query params (utm, ref, etc.)

Steven Teyf5c267f42665
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index 0278df9c03..445c3c9fb2 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -13,8 +13,7 @@ export default async function LinkMiddleware(
req: NextRequest,
ev: NextFetchEvent,
) {
- const url = req.nextUrl.clone();
- const { domain, fullKey: key } = parse(req);
+ const { domain, fullKey: key, query } = parse(req);
if (!domain || !key) {
return NextResponse.next();
@@ -46,15 +45,16 @@ export default async function LinkMiddleware(
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(ios, REDIRECT_HEADERS);
+ return NextResponse.redirect(ios + query, REDIRECT_HEADERS);
} else if (android && userAgent(req).os?.name === "Android") {
// redirect to Android link if it is specified and the user is on an Android device
- return NextResponse.redirect(android, REDIRECT_HEADERS);
+ return NextResponse.redirect(android + query, REDIRECT_HEADERS);
} else {
- return NextResponse.redirect(target, REDIRECT_HEADERS);
+ // regular redirect
+ return NextResponse.redirect(target + query, REDIRECT_HEADERS);
}
} else {
- url.pathname = "/";
- return NextResponse.redirect(url, REDIRECT_HEADERS);
+ // short link not found, redirect to root
+ return NextResponse.redirect(new URL("/", req.url), REDIRECT_HEADERS);
}
}
diff --git a/lib/middleware/utils.ts b/lib/middleware/utils.ts
index 49915fa13b..aaabc71f45 100644
--- a/lib/middleware/utils.ts
+++ b/lib/middleware/utils.ts
@@ -1,16 +1,22 @@
import { NextRequest } from "next/server";
-import { HOME_HOSTNAMES } from "@/lib/constants";
import { isHomeHostname } from "../utils";
export const parse = (req: NextRequest) => {
let domain = req.headers.get("host") as string;
domain = domain.replace("www.", ""); // remove www. from domain
if (isHomeHostname(domain)) domain = "dub.sh"; // if domain is a home hostname, set it to dub.sh
+
+ // path is the path of the URL (e.g. dub.sh/stats/github -> /stats/github)
const path = req.nextUrl.pathname;
- const key = decodeURIComponent(path.split("/")[1]); // decodeURIComponentto handle foreign languages like Hebrew
- const fullKey = decodeURIComponent(path).slice(1);
- return { domain, path, key, fullKey };
+ // Here, we are using decodeURIComponent to handle foreign languages like Hebrew
+ const key = decodeURIComponent(path.split("/")[1]); // key is the first part of the path (e.g. dub.sh/stats/github -> stats)
+ const fullKey = decodeURIComponent(path.slice(1)); // fullKey is the full path without the first slash (to account for multi-level subpaths, e.g. dub.sh/github/repo -> github/repo)
+
+ // query is the query string (e.g. dub.sh/github/repo?utm_source=twitter -> ?utm_source=twitter)
+ const query = req.nextUrl.search;
+
+ return { domain, path, key, fullKey, query };
};
export const detectBot = (req: NextRequest) => {
diff --git a/middleware.ts b/middleware.ts
index f8c010a984..1def1b1e7c 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -38,7 +38,7 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
}
// for public stats pages (e.g. dub.sh/stats/github)
- if (path.startsWith("/stats/")) {
+ if (key === "stats") {
return NextResponse.rewrite(new URL(`/${domain}${path}`, req.url));
}