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

Steven Tey739cf0ae62fa
diff --git a/apps/web/lib/api/links/bulk-create-links.ts b/apps/web/lib/api/links/bulk-create-links.ts
index 38656a11e4..0b121efd88 100644
--- a/apps/web/lib/api/links/bulk-create-links.ts
+++ b/apps/web/lib/api/links/bulk-create-links.ts
@@ -1,6 +1,11 @@
import { prisma } from "@/lib/prisma";
import { ProcessedLinkProps } from "@/lib/types";
-import { getParamsFromURL, linkConstructor, truncate } from "@dub/utils";
+import {
+ getParamsFromURL,
+ linkConstructor,
+ linkConstructorSimple,
+ truncate,
+} from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { propagateBulkLinkChanges } from "./propagate-bulk-link-changes";
import { updateLinksUsage } from "./update-links-usage";
@@ -103,7 +108,10 @@ export async function bulkCreateLinks({
return {
...link,
- shortLink: `https://${link.domain}/${link.key}`,
+ shortLink: linkConstructorSimple({
+ domain: link.domain,
+ key: link.key,
+ }),
title: truncate(link.title, 120),
description: truncate(link.description, 240),
utm_source,
@@ -120,7 +128,12 @@ export async function bulkCreateLinks({
createdLinks = await prisma.link.findMany({
where: {
shortLink: {
- in: links.map((link) => `https://${link.domain}/${link.key}`),
+ in: links.map((link) =>
+ linkConstructorSimple({
+ domain: link.domain,
+ key: link.key,
+ }),
+ ),
},
},
});
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index e292c218ef..5c70998bd2 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -10,6 +10,7 @@ import {
getParamsFromURL,
truncate,
} from "@dub/utils";
+import { linkConstructorSimple } from "@dub/utils/src/functions/link-constructor";
import { Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { updateLinksUsage } from "./update-links-usage";
@@ -29,7 +30,7 @@ export async function createLink(link: ProcessedLinkProps) {
data: {
...rest,
key,
- shortLink: `https://${link.domain}/${link.key}`,
+ shortLink: linkConstructorSimple({ domain: link.domain, key: link.key }),
title: truncate(title, 120),
description: truncate(description, 240),
// if it's an uploaded image, make this null first because we'll update it later
diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.ts
index 330731fb59..aa775c401a 100644
--- a/apps/web/lib/api/links/update-link.ts
+++ b/apps/web/lib/api/links/update-link.ts
@@ -3,7 +3,13 @@ import { isStored, storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird";
import { LinkProps, ProcessedLinkProps } from "@/lib/types";
import { formatRedisLink, redis } from "@/lib/upstash";
-import { R2_URL, getParamsFromURL, nanoid, truncate } from "@dub/utils";
+import {
+ R2_URL,
+ getParamsFromURL,
+ linkConstructorSimple,
+ nanoid,
+ truncate,
+} from "@dub/utils";
import { Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { combineTagIds, transformLink } from "./utils";
@@ -57,7 +63,10 @@ export async function updateLink({
data: {
...rest,
key,
- shortLink: `https://${updatedLink.domain}/${updatedLink.key}`,
+ shortLink: linkConstructorSimple({
+ domain: updatedLink.domain,
+ key: updatedLink.key,
+ }),
title: truncate(title, 120),
description: truncate(description, 240),
image:
diff --git a/apps/web/scripts/backfill-short-links.ts b/apps/web/scripts/backfill-short-links.ts
index f9469f2c6f..54ed9f9dfc 100644
--- a/apps/web/scripts/backfill-short-links.ts
+++ b/apps/web/scripts/backfill-short-links.ts
@@ -1,4 +1,5 @@
import { prisma } from "@/lib/prisma";
+import { linkConstructorSimple } from "@dub/utils";
import "dotenv-flow/config";
async function main() {
@@ -8,7 +9,7 @@ async function main() {
while (true) {
const links = await prisma.link.findMany({
where: {
- shortLink: null,
+ shortLink: "null",
},
select: {
id: true,
@@ -27,7 +28,10 @@ async function main() {
prisma.link.update({
where: { id: link.id },
data: {
- shortLink: `https://${link.domain}/${link.key}`,
+ shortLink: linkConstructorSimple({
+ domain: link.domain,
+ key: link.key,
+ }),
},
}),
),
diff --git a/packages/utils/src/functions/link-constructor.ts b/packages/utils/src/functions/link-constructor.ts
index 5c950dfac8..1bae69e541 100644
--- a/packages/utils/src/functions/link-constructor.ts
+++ b/packages/utils/src/functions/link-constructor.ts
@@ -27,3 +27,13 @@ export function linkConstructor({
return pretty ? url.replace(/^https?:\/\//, "") : url;
}
+
+export function linkConstructorSimple({
+ domain,
+ key,
+}: {
+ domain: string;
+ key: string;
+}) {
+ return `https://${domain}${key === "_root" ? "" : `/${key}`}`;
+}