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 crawler

Steven Tey95374a7e83d4
diff --git a/apps/web/app/api/links/crawl/bitly/route.ts b/apps/web/app/api/links/crawl/bitly/[domain]/[key]/route.ts
similarity index 94%
rename from apps/web/app/api/links/crawl/bitly/route.ts
rename to apps/web/app/api/links/crawl/bitly/[domain]/[key]/route.ts
index 8d026298cc..2b8a950b53 100644
--- a/apps/web/app/api/links/crawl/bitly/route.ts
+++ b/apps/web/app/api/links/crawl/bitly/[domain]/[key]/route.ts
@@ -1,22 +1,22 @@
import { redis } from "@/lib/upstash";
+import z from "@/lib/zod";
import { DUB_HEADERS, getUrlFromStringIfValid } from "@dub/utils";
import { NextRequest, NextResponse } from "next/server";
-import { z } from "zod";
// GET /api/links/crawl/bitly – crawl a bitly link and redirect to the destination if exists
-export const GET = async (req: NextRequest) => {
+export const GET = async (_req: NextRequest, { params }) => {
const { domain, key } = z
.object({
domain: z.string(),
key: z.string(),
})
- .parse(req.nextUrl.searchParams);
+ .parse(params);
// bitly doesn't support the following characters: ` ~ , . < > ; ‘ : “ / \ [ ] ^ { } ( ) = + ! * @ & $ £ ? % # |
// @see: https://support.bitly.com/hc/en-us/articles/360030780892-What-characters-are-supported-when-customizing-links
const invalidBitlyKeyRegex = /[`~,.<>;':"/\\[\]^{}()=+!*@&$£?%#|]/;
- if (domain === "buff.ly" && !invalidBitlyKeyRegex.test(key)) {
+ if (key && !invalidBitlyKeyRegex.test(key)) {
const link = await crawlBitlyLink({ domain, key });
if (link) {
const sanitizedUrl = getUrlFromStringIfValid(link.long_url);
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 80d795dda6..f64257cd30 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -85,11 +85,8 @@ export default async function LinkMiddleware(
if (!linkData) {
// special case for buff.ly
if (domain === "buff.ly") {
- return NextResponse.rewrite(
- new URL(
- `/api/links/crawl/bitly?domain=${domain}&key=${key}`,
- req.url,
- ),
+ return NextResponse.redirect(
+ new URL(`/api/links/crawl/bitly/${domain}/${key}`, req.url),
);
}