Skip to content

Commit

Permalink
Merge pull request dubinc#344 from steven-tey/debug-rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey authored Aug 29, 2023
2 parents 3037334 + d270726 commit 82cce41
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
27 changes: 17 additions & 10 deletions lib/middleware/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,39 @@ 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(decodeURIComponent(target), DUB_HEADERS);

// rewrite to proxy page (/_proxy/[domain]/[key]) if it's a bot and proxy is enabled
} else if (isBot && proxy) {
return NextResponse.rewrite(
new URL(`/proxy/${domain}/${encodeURIComponent(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
} else if (ios && userAgent(req).os?.name === "iOS") {
return NextResponse.redirect(getFinalUrl(ios, { req }), DUB_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
} else if (android && userAgent(req).os?.name === "Android") {
return NextResponse.redirect(getFinalUrl(android, { req }), DUB_HEADERS);
} else if (geo && country && country in geo) {

// 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,
);

// regular redirect
} else {
// regular redirect / rewrite
return rewrite
? NextResponse.rewrite(getFinalUrl(target, { req }), DUB_HEADERS)
: NextResponse.redirect(getFinalUrl(target, { req }), DUB_HEADERS);
return NextResponse.redirect(getFinalUrl(target, { req }), DUB_HEADERS);
}
} else {
// short link not found, redirect to root
Expand Down
5 changes: 4 additions & 1 deletion lib/middleware/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ const nextConfig = {
permanent: true,
statusCode: 301,
},
{
source: "/",
has: [
{
type: "host",
value: "staging.dub.sh",
},
],
destination: "https://dub.co",
permanent: true,
statusCode: 301,
},
{
source: "/",
has: [
Expand Down

0 comments on commit 82cce41

Please sign in to comment.