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.

fix api rewrite

Steven Teya7672b290c73
diff --git a/lib/middleware/api.ts b/lib/middleware/api.ts
index 6b068a4144..1772b19bc6 100644
--- a/lib/middleware/api.ts
+++ b/lib/middleware/api.ts
@@ -3,8 +3,8 @@ import { parse } from "#/lib/middleware/utils";
import { HOME_DOMAIN } from "../constants";
export default async function ApiMiddleware(req: NextRequest) {
- const { path } = parse(req);
- if (path.startsWith("/metatags")) {
+ const { fullPath } = parse(req);
+ if (fullPath.startsWith("/metatags")) {
const url = req.nextUrl.searchParams.get("url");
if (!url) {
return NextResponse.redirect(`${HOME_DOMAIN}/metatags`, {
@@ -15,5 +15,5 @@ export default async function ApiMiddleware(req: NextRequest) {
new URL(`/api/edge/metatags?url=${url}`, req.url),
);
}
- return NextResponse.rewrite(new URL(`/api${path}`, req.url));
+ return NextResponse.rewrite(new URL(`/api${fullPath}`, req.url));
}
diff --git a/lib/middleware/utils.ts b/lib/middleware/utils.ts
index ce0b48baea..0f481b2b47 100644
--- a/lib/middleware/utils.ts
+++ b/lib/middleware/utils.ts
@@ -9,11 +9,17 @@ export const parse = (req: NextRequest) => {
// path is the path of the URL (e.g. dub.sh/stats/github -> /stats/github)
const path = req.nextUrl.pathname;
+ // fullPath is the full URL path (along with search params)
+ const searchParams = req.nextUrl.searchParams.toString();
+ const fullPath = `${req.nextUrl.pathname}${
+ searchParams.length > 0 ? `?${searchParams}` : ""
+ }`;
+
// 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)
- return { domain, path, key, fullKey };
+ return { domain, path, fullPath, key, fullKey };
};
export const getFinalUrl = (target: string, { req }: { req: NextRequest }) => {