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.

Added changelog post, handle duplicate query params

Steven Tey945d5a0bc0c6
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index 445c3c9fb2..823b40599b 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -4,7 +4,7 @@ import {
NextResponse,
userAgent,
} from "next/server";
-import { detectBot, parse } from "@/lib/middleware/utils";
+import { detectBot, getFinalUrl, parse } from "@/lib/middleware/utils";
import { redis } from "@/lib/upstash";
import { recordClick } from "@/lib/tinybird";
import { REDIRECT_HEADERS } from "../constants";
@@ -13,7 +13,7 @@ export default async function LinkMiddleware(
req: NextRequest,
ev: NextFetchEvent,
) {
- const { domain, fullKey: key, query } = parse(req);
+ const { domain, fullKey: key } = parse(req);
if (!domain || !key) {
return NextResponse.next();
@@ -45,13 +45,19 @@ 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 + query, REDIRECT_HEADERS);
+ return NextResponse.redirect(getFinalUrl(ios, { req }), 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 + query, REDIRECT_HEADERS);
+ return NextResponse.redirect(
+ getFinalUrl(android, { req }),
+ REDIRECT_HEADERS,
+ );
} else {
// regular redirect
- return NextResponse.redirect(target + query, REDIRECT_HEADERS);
+ return NextResponse.redirect(
+ getFinalUrl(target, { req }),
+ REDIRECT_HEADERS,
+ );
}
} else {
// short link not found, redirect to root
diff --git a/lib/middleware/utils.ts b/lib/middleware/utils.ts
index aaabc71f45..436d9f4180 100644
--- a/lib/middleware/utils.ts
+++ b/lib/middleware/utils.ts
@@ -13,10 +13,25 @@ export const parse = (req: NextRequest) => {
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)
+ return { domain, path, key, fullKey };
+};
+
+export const getFinalUrl = (target: string, { req }: { req: NextRequest }) => {
// query is the query string (e.g. dub.sh/github/repo?utm_source=twitter -> ?utm_source=twitter)
- const query = req.nextUrl.search;
+ const searchParams = req.nextUrl.searchParams;
+
+ // get the query params of the target url
+ const targetUrl = new URL(target);
+
+ // if searchParams (type: `URLSearchParams`) has the same key as target url, then overwrite it
+ for (const [key, value] of searchParams) {
+ targetUrl.searchParams.set(key, value);
+ }
+
+ // construct final url
+ const finalUrl = targetUrl.toString();
- return { domain, path, key, fullKey, query };
+ return finalUrl;
};
export const detectBot = (req: NextRequest) => {
diff --git a/posts/changelog/pass-url-parameters.mdx b/posts/changelog/pass-url-parameters.mdx
new file mode 100644
index 0000000000..79ec8a617c
--- /dev/null
+++ b/posts/changelog/pass-url-parameters.mdx
@@ -0,0 +1,16 @@
+---
+title: Pass URL Parameters to Destination URL
+publishedAt: 2023-06-07
+summary: "You can now pass URL query parameters from your short link to the destination URL."
+image: "https://d2vwwcvoksz7ty.cloudfront.net/changelog/pass-url-parameters.png"
+author: "steventey"
+---
+
+You can now pass URL query parameters (`utm_source`, `ref`, etc.) from your short link to the destination URL.
+
+If there are any duplicate parameters, the parameter in the short link will override its counterpart the destination URL.
+
+For example:
+
+- [dub.sh/oss](https://dub.sh/oss) redirects to `vercel.com/oss?ref=dub`
+- [dub.sh/oss?ref=dub.sh](https://dub.sh/oss?ref=dub.sh) redirects to `vercel.com/oss?ref=dub.sh`