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.

remove status code for rewrites

Steven Tey8b2fe70f2bc2
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 1217c8eb25..6c78eb71a8 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -57,7 +57,10 @@ export default async function LinkMiddleware(
if (!linkData) {
// short link not found, redirect to root
// TODO: log 404s (https://github.com/dubinc/dub/issues/559)
- return NextResponse.redirect(new URL("/", req.url), DUB_HEADERS);
+ return NextResponse.redirect(new URL("/", req.url), {
+ ...DUB_HEADERS,
+ status: 302,
+ });
}
// format link to fit the RedisLinkProps interface
@@ -157,21 +160,30 @@ export default async function LinkMiddleware(
// 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 }), DUB_HEADERS);
+ return NextResponse.redirect(getFinalUrl(ios, { req }), {
+ ...DUB_HEADERS,
+ 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 }), DUB_HEADERS);
+ return NextResponse.redirect(getFinalUrl(android, { req }), {
+ ...DUB_HEADERS,
+ 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 }),
- DUB_HEADERS,
- );
+ return NextResponse.redirect(getFinalUrl(geo[country], { req }), {
+ ...DUB_HEADERS,
+ status: 302,
+ });
// regular redirect
} else {
- return NextResponse.redirect(getFinalUrl(url, { req }), DUB_HEADERS);
+ return NextResponse.redirect(getFinalUrl(url, { req }), {
+ ...DUB_HEADERS,
+ status: 302,
+ });
}
}
diff --git a/apps/web/lib/middleware/root.ts b/apps/web/lib/middleware/root.ts
index 06831ffd14..4bcdf361b1 100644
--- a/apps/web/lib/middleware/root.ts
+++ b/apps/web/lib/middleware/root.ts
@@ -23,7 +23,7 @@ export default async function RootMiddleware(
if (!linkData) {
// rewrite to placeholder page if domain doesn't exist
- return NextResponse.rewrite(new URL(`/${domain}`, req.url));
+ return NextResponse.rewrite(new URL(`/${domain}`, req.url), DUB_HEADERS);
}
// format link to fit the RedisLinkProps interface
@@ -57,8 +57,7 @@ export default async function RootMiddleware(
return NextResponse.rewrite(url, DUB_HEADERS);
}
} else {
- // Dub redirects are 302 by default, but for root links that have a destination URL,
- // we want to use 301 (for SEO purposes)
+ // For root links that have a destination URL, use 301 status code (for SEO purposes)
return NextResponse.redirect(url, { ...DUB_HEADERS, status: 301 });
}
}
diff --git a/packages/utils/src/constants/middleware.ts b/packages/utils/src/constants/middleware.ts
index 660de8df04..d602ca812b 100644
--- a/packages/utils/src/constants/middleware.ts
+++ b/packages/utils/src/constants/middleware.ts
@@ -17,5 +17,4 @@ export const DUB_HEADERS = {
headers: {
"x-powered-by": "Dub.co - Link management for modern marketing teams",
},
- status: 302,
};