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.

remove encode and decodeURIComponent

Steven Tey7e21fd944efe
diff --git a/apps/web/app/api/links/sync/route.ts b/apps/web/app/api/links/sync/route.ts
index 251b58baca..02c4377451 100644
--- a/apps/web/app/api/links/sync/route.ts
+++ b/apps/web/app/api/links/sync/route.ts
@@ -3,6 +3,7 @@ import prisma from "@/lib/prisma";
import { NextResponse } from "next/server";
import { LinkProps, SimpleLinkProps } from "@/lib/types";
import { exceededLimitError } from "@/lib/api/errors";
+import { bulkCreateLinks } from "@/lib/api/links";
// POST /api/links/sync – sync user's publicly created links to their accounts
export const POST = withAuth(async ({ req, session, project }) => {
@@ -58,6 +59,7 @@ export const POST = withAuth(async ({ req, session, project }) => {
publicStats: false,
},
}),
+ bulkCreateLinks(unclaimedLinks),
prisma.project.update({
where: {
id: project.id,
diff --git a/apps/web/lib/api/domains.ts b/apps/web/lib/api/domains.ts
index 680d991c67..732f19e61e 100644
--- a/apps/web/lib/api/domains.ts
+++ b/apps/web/lib/api/domains.ts
@@ -245,20 +245,9 @@ export async function deleteDomainAndLinks(
skipPrismaDelete = false,
} = {},
) {
- const links = await prisma.link.findMany({
- where: {
- domain,
- },
- });
-
- const pipeline = redis.pipeline();
- links.forEach(({ key }) => {
- pipeline.del(`${domain}:${key}`);
- });
- pipeline.del(`root:${domain}`);
-
return await Promise.allSettled([
- pipeline.exec(), // delete all links from redis
+ // delete all links from redis
+ redis.del(domain),
// remove all images from cloudinary
cloudinary.v2.api.delete_resources_by_prefix(domain),
// remove the domain from Vercel
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 47ff01b0ae..281472da18 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -604,7 +604,7 @@ export async function editLink({
invalidate: true,
})
.catch(() => {}),
- redis.del(`${oldDomain}:${oldKey}`),
+ redis.hdel(oldDomain, oldKey.toLowerCase()),
]
: []),
]);
@@ -651,7 +651,7 @@ export async function deleteLink({
cloudinary.v2.uploader.destroy(`${domain}/${key}`, {
invalidate: true,
}),
- redis.del(`${domain}:${key}`),
+ redis.hdel(domain, key.toLowerCase()),
projectId &&
prisma.project.update({
where: {
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 0174f03c8e..2b9cb27d79 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -6,7 +6,6 @@ import {
LEGAL_PROJECT_ID,
LOCALHOST_GEO_DATA,
LOCALHOST_IP,
- isDubDomain,
} from "@dub/utils";
import { ipAddress } from "@vercel/edge";
import {
@@ -56,17 +55,9 @@ export default async function LinkMiddleware(
key = key.slice(0, -1);
}
- const response = await redis.hmget<
- Record<string, RedisLinkProps | RedisDomainMetadataProps>
- >(domain, key, "_metadata");
+ let link = await redis.hget<RedisLinkProps>(domain, key);
- let link = response ? (response[key] as RedisLinkProps) : null;
-
- let metadata = response
- ? (response._metadata as RedisDomainMetadataProps)
- : null;
-
- if (!metadata || !link) {
+ if (!link) {
const linkData = await getLinkViaEdge(domain, key);
if (!linkData) {
@@ -74,20 +65,11 @@ 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;
- metadata = {
- projectId: linkData.projectId,
- };
- }
// format link to fit the RedisLinkProps interface
link = await getRedisLink(linkData as any);
await redis.hset(domain, {
[key]: link,
- ...(setMetadata && { _metadata: metadata }),
});
}
@@ -102,13 +84,9 @@ export default async function LinkMiddleware(
ios,
android,
geo,
+ projectId,
} = 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(
@@ -146,7 +124,7 @@ export default async function LinkMiddleware(
// only track the click when there is no `dub-no-track` header
if (!req.headers.get("dub-no-track")) {
- ev.waitUntil(recordClick({ req, id, domain, key }));
+ ev.waitUntil(recordClick({ req, id, domain, key, projectId }));
}
const isBot = detectBot(req);
@@ -169,7 +147,7 @@ export default async function LinkMiddleware(
);
} else {
// if link is not iframeable, use Next.js rewrite instead
- return NextResponse.rewrite(decodeURIComponent(target), DUB_HEADERS);
+ return NextResponse.rewrite(target, DUB_HEADERS);
}
// redirect to iOS link if it is specified and the user is on an iOS device
diff --git a/apps/web/lib/middleware/utils.ts b/apps/web/lib/middleware/utils.ts
index dfe736afe9..baa3abe2ef 100644
--- a/apps/web/lib/middleware/utils.ts
+++ b/apps/web/lib/middleware/utils.ts
@@ -30,7 +30,7 @@ export const getFinalUrl = (target: string, { req }: { req: NextRequest }) => {
const searchParams = req.nextUrl.searchParams;
// get the query params of the target url
- const targetUrl = new URL(decodeURIComponent(target));
+ const targetUrl = new URL(target);
// @ts-ignore – until https://github.com/microsoft/TypeScript/issues/54466 is fixed
if (searchParams.size === 0) return targetUrl.toString(); // if there are no query params, then return the target url as is (no need to parse it)
diff --git a/apps/web/lib/tinybird.ts b/apps/web/lib/tinybird.ts
index 596468c2a7..e455ec0822 100644
--- a/apps/web/lib/tinybird.ts
+++ b/apps/web/lib/tinybird.ts
@@ -19,11 +19,13 @@ export async function recordClick({
id,
domain,
key,
+ projectId,
}: {
req: NextRequest;
id?: string;
domain: string;
key?: string;
+ projectId?: string;
}) {
const isBot = detectBot(req);
if (isBot) {
diff --git a/apps/web/lib/upstash.ts b/apps/web/lib/upstash.ts
index cded0dfee3..cfc8d078a4 100644
--- a/apps/web/lib/upstash.ts
+++ b/apps/web/lib/upstash.ts
@@ -74,7 +74,7 @@ export async function getRedisLink(link: LinkProps): Promise<RedisLinkProps> {
return {
id,
- url: encodeURIComponent(url),
+ url,
...(hasPassword && { password: true }),
...(proxy && { proxy: true }),
...(rewrite && {
@@ -85,7 +85,6 @@ 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 }),
+ ...(projectId && { projectId }),
};
}