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.

Merge branch 'main' into conversion

Steven Teyc98f403a3dc8
diff --cc apps/web/lib/middleware/link.ts
index d147717332,7703af19cf..a4abf12b36
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@@ -6,7 -6,7 +6,8 @@@ import
DUB_HEADERS,
LEGAL_WORKSPACE_ID,
LOCALHOST_GEO_DATA,
+ isDubDomain,
+ nanoid,
punyEncode,
} from "@dub/utils";
import {
@@@ -126,15 -145,21 +147,22 @@@ export default async function LinkMiddl
// if the link has expired
if (expiresAt && new Date(expiresAt) < new Date()) {
if (expiredUrl) {
- return NextResponse.redirect(expiredUrl, DUB_HEADERS);
+ return NextResponse.redirect(expiredUrl, {
+ headers: {
+ ...DUB_HEADERS,
+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
+ },
+ });
} else {
- return NextResponse.rewrite(
- new URL(`/expired/${domain}`, req.url),
- DUB_HEADERS,
- );
+ return NextResponse.rewrite(new URL(`/expired/${domain}`, req.url), {
+ headers: {
+ ...DUB_HEADERS,
+ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
+ },
+ });
}
}
+ const clickId = nanoid(16);
const searchParams = req.nextUrl.searchParams;
// only track the click when there is no `dub-no-track` header or query param
@@@ -182,54 -227,42 +232,66 @@@
// redirect to iOS link if it is specified and the user is on an iOS device
} else if (ios && userAgent(req).os?.name === "iOS") {
- return NextResponse.redirect(getFinalUrl(ios, { req }), {
- headers: {
- ...DUB_HEADERS,
- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
+ return NextResponse.redirect(
+ getFinalUrl(ios, {
+ req,
+ clickId: trackConversion ? clickId : undefined,
+ }),
+ {
- ...DUB_HEADERS,
++ headers: {
++ ...DUB_HEADERS,
++ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
++ },
+ status: 302,
},
- status: 302,
- });
+ );
// redirect to Android link if it is specified and the user is on an Android device
} else if (android && userAgent(req).os?.name === "Android") {
- return NextResponse.redirect(getFinalUrl(android, { req }), {
- headers: {
- ...DUB_HEADERS,
- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
+ return NextResponse.redirect(
+ getFinalUrl(android, {
+ req,
+ clickId: trackConversion ? clickId : undefined,
+ }),
+ {
- ...DUB_HEADERS,
++ headers: {
++ ...DUB_HEADERS,
++ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
++ },
+ status: 302,
},
- status: 302,
- });
+ );
// redirect to geo-specific link if it is specified and the user is in the specified country
} else if (geo && country && country in geo) {
- return NextResponse.redirect(getFinalUrl(geo[country], { req }), {
- headers: {
- ...DUB_HEADERS,
- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
+ return NextResponse.redirect(
+ getFinalUrl(geo[country], {
+ req,
+ clickId: trackConversion ? clickId : undefined,
+ }),
+ {
- ...DUB_HEADERS,
++ headers: {
++ ...DUB_HEADERS,
++ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
++ },
+ status: 302,
},
- status: 302,
- });
+ );
// regular redirect
} else {
- return NextResponse.redirect(getFinalUrl(url, { req }), {
- headers: {
- ...DUB_HEADERS,
- ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
+ return NextResponse.redirect(
+ getFinalUrl(url, {
+ req,
+ clickId: trackConversion ? clickId : undefined,
+ }),
+ {
- ...DUB_HEADERS,
++ headers: {
++ ...DUB_HEADERS,
++ ...(!isDubDomain(domain) && { "X-Robots-Tag": "googlebot: noindex" }),
++ },
+ status: 302,
},
- status: 302,
- });
+ );
}
}
diff --cc apps/web/lib/middleware/root.ts
index 9923f6b038,cf63466475..7802b09188
--- a/apps/web/lib/middleware/root.ts
+++ b/apps/web/lib/middleware/root.ts
@@@ -36,10 -40,10 +40,10 @@@ export default async function RootMiddl
);
}
- const { id: linkId, url, rewrite, iframeable } = link;
- const { id, url, rewrite, iframeable, noindex } = link;
++ const { id: linkId, url, rewrite, iframeable, noindex } = link;
// record clicks on root page
- ev.waitUntil(recordClick({ req, id, ...(url && { url }), root: true }));
+ ev.waitUntil(recordClick({ req, linkId, ...(url && { url }), root: true }));
if (!url) {
// rewrite to placeholder page unless the user defines a site to redirect to