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 on-demand link generation

Steven Teyb3125b9f0764
diff --git a/apps/web/lib/middleware/utils/crawl-bitly.ts b/apps/web/lib/middleware/utils/crawl-bitly.ts
index aa218000cc..de2d6af5cf 100644
--- a/apps/web/lib/middleware/utils/crawl-bitly.ts
+++ b/apps/web/lib/middleware/utils/crawl-bitly.ts
@@ -1,19 +1,70 @@
+import { createId } from "@/lib/api/create-id";
import { linkCache } from "@/lib/api/links/cache";
-import { redis } from "@/lib/upstash";
-import { DUB_HEADERS } from "@dub/utils";
+import { encodeKeyIfCaseSensitive } from "@/lib/api/links/case-sensitivity";
+import { recordLink } from "@/lib/tinybird";
+import { prisma } from "@dub/prisma";
+import {
+ DUB_HEADERS,
+ getUrlFromStringIfValid,
+ linkConstructorSimple,
+} from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
import { NextRequest, NextResponse } from "next/server";
import { parse } from "./parse";
+const BUFFER_WORKSPACE_ID = "cm05wnnpo000711ztj05wwdbu";
+const BUFFER_USER_ID = "cm05wnd49000411ztg2xbup0i";
+const BUFFER_FOLDER_ID = "fold_LIZsdjTgFVbQVGYSUmYAi5vT";
+const BUFFER_BITLY_API_KEY = process.env.BUFFER_BITLY_API_KEY;
+
export const crawlBitly = async (req: NextRequest) => {
- const { domain, fullKey } = parse(req);
+ const { domain, fullKey: key } = parse(req);
- // bitly doesn't support the following characters: ` ~ , . < > ; ‘ : “ / \ [ ] ^ { } ( ) = + ! * @ & $ £ ? % # |
+ // 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 (fullKey && !invalidBitlyKeyRegex.test(fullKey)) {
- const link = await fetchBitlyLink({ domain, key: fullKey });
+ if (key && !invalidBitlyKeyRegex.test(key)) {
+ const link = await fetchBitlyLink({ domain, key });
if (link) {
+ const sanitizedUrl = getUrlFromStringIfValid(link.long_url);
+ if (sanitizedUrl) {
+ console.log(
+ `[Bitly] Creating link on-demand: ${domain}/${key} (createdAt: ${link.created_at})`,
+ );
+ const encodedKey = encodeKeyIfCaseSensitive({ domain, key });
+ waitUntil(
+ prisma.link
+ .create({
+ data: {
+ id: createId({ prefix: "link_" }),
+ domain,
+ key: encodedKey,
+ url: sanitizedUrl,
+ shortLink: linkConstructorSimple({ domain, key: encodedKey }),
+ projectId: BUFFER_WORKSPACE_ID,
+ userId: BUFFER_USER_ID,
+ folderId: BUFFER_FOLDER_ID,
+ createdAt: new Date(link.created_at),
+ },
+ })
+ .then((data) =>
+ Promise.allSettled([
+ // console log outputs
+ recordLink(data),
+ prisma.project.update({
+ where: {
+ id: BUFFER_WORKSPACE_ID,
+ },
+ data: {
+ linksUsage: { increment: 1 },
+ },
+ }),
+ ]),
+ ),
+ );
+ }
+
return NextResponse.redirect(link.long_url, {
headers: DUB_HEADERS,
status: 302,
@@ -27,15 +78,13 @@ export const crawlBitly = async (req: NextRequest) => {
"Vercel-CDN-Cache-Control": "public, s-maxage=86400",
"Vercel-Cache-Tag": linkCache._createStaticPagesCacheKeys({
domain,
- key: fullKey,
+ key,
}),
},
status: 302,
});
};
-const BUFFER_WORKSPACE_ID = "cm05wnnpo000711ztj05wwdbu";
-
async function fetchBitlyLink({
domain,
key,
@@ -43,20 +92,11 @@ async function fetchBitlyLink({
domain: string;
key: string;
}) {
- const apiKey = await redis.get<string>(`import:bitly:${BUFFER_WORKSPACE_ID}`);
-
- if (!apiKey) {
- console.error(
- `[Bitly] No API key found for workspace ${BUFFER_WORKSPACE_ID}`,
- );
- return null;
- }
-
const response = await fetch(
`https://api-ssl.bitly.com/v4/bitlinks/${domain}/${key}`,
{
headers: {
- Authorization: `Bearer ${apiKey}`,
+ Authorization: `Bearer ${BUFFER_BITLY_API_KEY}`,
},
},
);