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.

update link proxy

Kiran K2ff787877246
diff --git a/apps/web/app/api/og/analytics/route.tsx b/apps/web/app/api/og/analytics/route.tsx
index dcbad6cc99..bb6f3f1b0e 100644
--- a/apps/web/app/api/og/analytics/route.tsx
+++ b/apps/web/app/api/og/analytics/route.tsx
@@ -19,7 +19,7 @@ export async function GET(req: NextRequest) {
const domain = req.nextUrl.searchParams.get("domain") || "dub.sh";
const key = req.nextUrl.searchParams.get("key") || "github";
- const link = await getLinkViaEdge(domain, key);
+ const link = await getLinkViaEdge({ domain, key });
if (!link?.publicStats) {
return new Response(`Stats for this link are not public`, {
status: 403,
diff --git a/apps/web/app/inspect/[domain]/[key]/page.tsx b/apps/web/app/inspect/[domain]/[key]/page.tsx
index d47516d6c9..0242f41f19 100644
--- a/apps/web/app/inspect/[domain]/[key]/page.tsx
+++ b/apps/web/app/inspect/[domain]/[key]/page.tsx
@@ -27,7 +27,7 @@ export async function generateMetadata({
const domain = params.domain;
const key = decodeURIComponent(params.key).slice(0, -1);
- const data = await getLinkViaEdge(domain, key);
+ const data = await getLinkViaEdge({ domain, key });
if (!data) {
return;
@@ -52,7 +52,7 @@ export default async function InspectPage({
const domain = params.domain;
const key = decodeURIComponent(params.key).slice(0, -1);
- const data = await getLinkViaEdge(domain, key);
+ const data = await getLinkViaEdge({ domain, key });
// if the link doesn't exist
if (!data) {
diff --git a/apps/web/app/proxy/[domain]/[key]/page.tsx b/apps/web/app/proxy/[domain]/[key]/page.tsx
index d11e5d3fa1..1fa6966ac0 100644
--- a/apps/web/app/proxy/[domain]/[key]/page.tsx
+++ b/apps/web/app/proxy/[domain]/[key]/page.tsx
@@ -18,7 +18,7 @@ export async function generateMetadata({
const domain = params.domain;
const key = decodeURIComponent(params.key); // key can potentially be encoded
- const data = await getLinkViaEdge(domain, key);
+ const data = await getLinkViaEdge({ domain, key });
if (!data?.proxy) {
return;
@@ -44,7 +44,7 @@ export default async function ProxyPage({
const domain = params.domain;
const key = decodeURIComponent(params.key);
- const data = await getLinkViaEdge(domain, key);
+ const data = await getLinkViaEdge({ domain, key });
// if the link doesn't exist
if (!data) {
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 3fe5e589e7..a41d2e975b 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -78,15 +78,19 @@ export default async function LinkMiddleware(
let link = await linkCache.get({ domain, key });
if (!link) {
- let linkData = await getLinkViaEdge(domain, key);
+ let linkData = await getLinkViaEdge({ domain, key });
if (!linkData) {
- // TODO
- // if the link doesn’t exist in redis (which is case sensitive), we need to read both the old version + new hashed version from MySQL.
- // If the hashed version exists, we should use that one since we know that it’s 100% accurate.
-
if (domain === "buff.ly" || domain === "dev.buff.ly") {
- linkData = await importBitlyLink({ domain, key: originalKey });
+ linkData = await getLinkViaEdge({
+ domain,
+ key: originalKey,
+ caseSensitive: false,
+ });
+
+ if (!linkData) {
+ linkData = await importBitlyLink({ domain, key: originalKey });
+ }
}
if (!linkData) {
@@ -163,7 +167,7 @@ export default async function LinkMiddleware(
// - no `pw` param is provided
// - the `pw` param is incorrect
// this will also ensure that no clicks are tracked unless the password is correct
- if (!pw || (await getLinkViaEdge(domain, key))?.password !== pw) {
+ if (!pw || (await getLinkViaEdge({ domain, key })).password !== pw) {
return NextResponse.rewrite(new URL(`/password/${linkId}`, req.url), {
headers: {
...DUB_HEADERS,
diff --git a/apps/web/lib/planetscale/get-link-via-edge.ts b/apps/web/lib/planetscale/get-link-via-edge.ts
index fd4af1be4b..55d8ea34f7 100644
--- a/apps/web/lib/planetscale/get-link-via-edge.ts
+++ b/apps/web/lib/planetscale/get-link-via-edge.ts
@@ -6,11 +6,21 @@ import {
import { conn } from "./connection";
import { EdgeLinkProps } from "./types";
-export const getLinkViaEdge = async (domain: string, key: string) => {
- const decodedKey = decodeKeyIfCaseSensitive({
- domain,
- key,
- });
+export const getLinkViaEdge = async ({
+ domain,
+ key,
+ caseSensitive = true,
+}: {
+ domain: string;
+ key: string;
+ caseSensitive?: boolean;
+}) => {
+ const decodedKey = caseSensitive
+ ? decodeKeyIfCaseSensitive({
+ domain,
+ key,
+ })
+ : key;
const { rows } =
(await conn.execute(
@@ -23,5 +33,5 @@ export const getLinkViaEdge = async (domain: string, key: string) => {
? (rows[0] as EdgeLinkProps)
: null;
- return link ? decodeLinkIfCaseSensitive(link) : null;
+ return link ? (caseSensitive ? decodeLinkIfCaseSensitive(link) : link) : null;
};