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.

fallback to bit.ly

Kiran Kaadd35312036
diff --git a/apps/web/lib/middleware/bitly.ts b/apps/web/lib/middleware/bitly.ts
index fb2e88c593..5c05caf859 100644
--- a/apps/web/lib/middleware/bitly.ts
+++ b/apps/web/lib/middleware/bitly.ts
@@ -16,8 +16,10 @@ type BitlyLink = {
tags: string[];
};
+// https://buff.ly/4hYdRCC https://bit.ly/4hYdRCC
+
// Create a new Bitly link in Dub on-demand
-export const importBitlyLink = async (bitlyLink: string) => {
+export const importBitlyLink = async (shortKey: string) => {
const workspaceId = "";
const userId = "";
const folderId = "";
@@ -29,8 +31,9 @@ export const importBitlyLink = async (bitlyLink: string) => {
return null;
}
- const response = await fetch(
- `https://api-ssl.bitly.com/v4/bitlinks/${bitlyLink}`,
+ // First try buff.ly
+ let response = await fetch(
+ `https://api-ssl.bitly.com/v4/bitlinks/buff.ly/${shortKey}`,
{
headers: {
Authorization: `Bearer ${bitlyApiKey}`,
@@ -38,11 +41,25 @@ export const importBitlyLink = async (bitlyLink: string) => {
},
);
- const data = await response.json();
+ let data = await response.json();
+ // If the link is not found, try bit.ly
if (!response.ok) {
- console.error(`[Bitly] Error retrieving Bitly link: ${bitlyLink}`, data);
- return null;
+ response = await fetch(
+ `https://api-ssl.bitly.com/v4/bitlinks/bit.ly/${shortKey}`,
+ {
+ headers: {
+ Authorization: `Bearer ${bitlyApiKey}`,
+ },
+ },
+ );
+
+ if (!response.ok) {
+ console.error("[Bitly] Error retrieving Bitly link", data);
+ return null;
+ }
+
+ data = await response.json();
}
const link = data as BitlyLink;
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index fd39c114b1..55c2056951 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -26,6 +26,7 @@ import {
import { linkCache } from "../api/links/cache";
import { getLinkViaEdge } from "../planetscale";
import { getDomainViaEdge } from "../planetscale/get-domain-via-edge";
+import { importBitlyLink } from "./bitly";
import { hasEmptySearchParams } from "./utils/has-empty-search-params";
export default async function LinkMiddleware(
@@ -71,6 +72,10 @@ export default async function LinkMiddleware(
let linkData = await getLinkViaEdge(domain, key);
if (!linkData) {
+ if (domain === "buff.ly") {
+ linkData = await importBitlyLink(key);
+ }
+
if (!linkData) {
// check if domain has notFoundUrl configured
const domainData = await getDomainViaEdge(domain);