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.

small edits

Steven Tey097a8a5f058a
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 335f976502..47ff01b0ae 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -405,12 +405,8 @@ export async function addLink(link: LinkProps) {
description,
image,
proxy,
- rewrite,
- ios,
- android,
geo,
} = link;
- const hasPassword = password && password.length > 0 ? true : false;
const uploadedImage = image && image.startsWith("data:image") ? true : false;
const exists = await checkIfKeyExists(domain, key);
@@ -546,17 +542,12 @@ export async function editLink({
key,
url,
expiresAt,
- password,
title,
description,
image,
proxy,
- rewrite,
- ios,
- android,
geo,
} = updatedLink;
- const hasPassword = password && password.length > 0 ? true : false;
const changedKey = key.toLowerCase() !== oldKey.toLowerCase();
const changedDomain = domain !== oldDomain;
const uploadedImage = image && image.startsWith("data:image") ? true : false;
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index e619c85c19..0174f03c8e 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -6,6 +6,7 @@ import {
LEGAL_PROJECT_ID,
LOCALHOST_GEO_DATA,
LOCALHOST_IP,
+ isDubDomain,
} from "@dub/utils";
import { ipAddress } from "@vercel/edge";
import {
@@ -16,7 +17,7 @@ import {
} from "next/server";
import { isBlacklistedReferrer } from "../edge-config";
import { getLinkViaEdge } from "../planetscale";
-import { LinkProps, RedisDomainMetadataProps, RedisLinkProps } from "../types";
+import { RedisDomainMetadataProps, RedisLinkProps } from "../types";
export default async function LinkMiddleware(
req: NextRequest,
@@ -50,25 +51,21 @@ export default async function LinkMiddleware(
}
const inspectMode = key.endsWith("+");
+ // if inspect mode is enabled, remove the trailing `+` from the key
if (inspectMode) {
key = key.slice(0, -1);
}
const response = await redis.hmget<
- Record<string, RedisDomainMetadataProps | RedisLinkProps>
- >(
- domain,
- "_metadata",
- // if inspect mode is enabled, remove the trailing `+` from the key
- key,
- );
+ Record<string, RedisLinkProps | RedisDomainMetadataProps>
+ >(domain, key, "_metadata");
+
+ let link = response ? (response[key] as RedisLinkProps) : null;
let metadata = response
? (response._metadata as RedisDomainMetadataProps)
: null;
- let link = response ? (response[key] as RedisLinkProps) : null;
-
if (!metadata || !link) {
const linkData = await getLinkViaEdge(domain, key);
@@ -77,6 +74,7 @@ export default async function LinkMiddleware(
return NextResponse.redirect(new URL("/", req.url), DUB_HEADERS);
}
+ // only set metadata if it's not been set before
let setMetadata = false;
if (!metadata) {
setMetadata = true;
@@ -84,11 +82,12 @@ export default async function LinkMiddleware(
projectId: linkData.projectId,
};
}
+ // format link to fit the RedisLinkProps interface
link = await getRedisLink(linkData as any);
await redis.hset(domain, {
- ...(setMetadata && { _metadata: metadata }),
[key]: link,
+ ...(setMetadata && { _metadata: metadata }),
});
}
@@ -105,6 +104,11 @@ export default async function LinkMiddleware(
geo,
} = link;
+ const projectId = isDubDomain(domain) ? link.projectId : metadata.projectId;
+ const rules = metadata.redirectRules;
+
+ console.log({ link, metadata, projectId, rules });
+
// only show inspect modal if the link is not password protected
if (inspectMode && !password) {
return NextResponse.rewrite(
@@ -131,7 +135,7 @@ export default async function LinkMiddleware(
}
// if the link is banned
- if (metadata.projectId === LEGAL_PROJECT_ID) {
+ if (link.projectId === LEGAL_PROJECT_ID) {
return NextResponse.rewrite(new URL("/banned", req.url));
}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 78f30677d9..338fec723f 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -29,6 +29,7 @@ export interface RedisLinkProps {
ios?: string;
android?: string;
geo?: object;
+ projectId?: string;
}
export interface TagProps {
diff --git a/apps/web/lib/upstash.ts b/apps/web/lib/upstash.ts
index 87815d1c5b..cded0dfee3 100644
--- a/apps/web/lib/upstash.ts
+++ b/apps/web/lib/upstash.ts
@@ -1,4 +1,4 @@
-import { getDomainWithoutWWW, isIframeable } from "@dub/utils";
+import { getDomainWithoutWWW, isDubDomain, isIframeable } from "@dub/utils";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { LinkProps, RedisLinkProps } from "./types";
@@ -68,6 +68,7 @@ export async function getRedisLink(link: LinkProps): Promise<RedisLinkProps> {
ios,
android,
geo,
+ projectId,
} = link;
const hasPassword = password && password.length > 0 ? true : false;
@@ -84,5 +85,7 @@ export async function getRedisLink(link: LinkProps): Promise<RedisLinkProps> {
...(ios && { ios }),
...(android && { android }),
...(geo && { geo: geo as object }),
+ // we record the projectId for default Dub domains
+ ...(isDubDomain(domain) && projectId && { projectId }),
};
}