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.

UTM Builder

Steven Tey703a1cf7bc68
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index 5dad425e7a..d4bf57d756 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -27,7 +27,7 @@ export default async function LinkMiddleware(
const noTrack = req.headers.get("dub-no-track");
if (!noTrack) ev.waitUntil(recordClick(hostname, req, key)); // track the click only if there is no `dub-no-track` header
- if (image && description && isBot) {
+ if (isBot) {
// rewrite to proxy page (dub.sh/proxy/[domain]/[key])
return NextResponse.rewrite(`https://dub.sh/proxy/${hostname}/${key}`);
} else {
diff --git a/pages/proxy/[domain]/[key].tsx b/pages/proxy/[domain]/[key].tsx
index eea7ad2bd8..06c7fb480d 100644
--- a/pages/proxy/[domain]/[key].tsx
+++ b/pages/proxy/[domain]/[key].tsx
@@ -64,16 +64,27 @@ export function getStaticPaths() {
export async function getStaticProps(ctx) {
const { domain, key } = ctx.params;
- const response = await redis.hget<Omit<LinkProps, "key">>(
- `${domain}:links`,
- key,
- );
- const { url, title, description, image } = response || {};
- if (!url || !title || !description || !image) {
+ const link = await prisma.link.findUnique({
+ where: {
+ domain,
+ key,
+ },
+ });
+
+ const { url, title, description, image } = link || {};
+
+ if (!url) {
return {
notFound: true,
revalidate: 1,
};
+ } else if (!image) {
+ return {
+ redirect: {
+ destination: url,
+ },
+ revalidate: 1,
+ };
}
const hostname = new URL(url).hostname;
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 57615104cf..b8c9336bb6 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -26,6 +26,7 @@ model User {
sessions Session[]
projects ProjectUsers[]
sentEmails SentEmail[]
+ links Link[]
createdAt DateTime @default(now())
}
@@ -79,6 +80,7 @@ model Project {
ownerUsageLimit Int @default(1000)
ownerExceededUsage Boolean @default(false)
sentEmails SentEmail[]
+ links Link[]
}
model ProjectUsers {
@@ -109,4 +111,35 @@ model SentEmail {
userId String?
project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
projectId String?
+}
+
+model Link {
+ id String @id @default(cuid())
+ domain String
+ key String
+ url String
+ title String?
+ description String?
+ image String?
+ utm_source String?
+ utm_medium String?
+ utm_campaign String?
+ clicks Int @default(0)
+ user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
+ userId String?
+ project Project? @relation(fields: [domain], references: [domain], onUpdate: Cascade, onDelete: Cascade)
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+
+ @@unique([domain, key])
+
+ // indices for links per domain/project
+ @@index([domain, createdAt(sort: Desc)])
+ @@index([domain, clicks(sort: Desc)])
+
+ // indices for links per domain/project specific to a user (only for dub.sh)
+ @@index([domain, userId, createdAt(sort: Desc)])
+ @@index([domain, userId, clicks(sort: Desc)])
+
+ // TODO: might wanna add indices for utm_source, utm_medium, utm_campaign
}
\ No newline at end of file