The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
update schema, split upstash into own files
Steven Tey6a6f5148b6ff
diff --git a/apps/web/app/proxy/[domain]/[key]/page.tsx b/apps/web/app/proxy/[domain]/[key]/page.tsxindex 91c99dc9a3..9b7b72374a 100644--- a/apps/web/app/proxy/[domain]/[key]/page.tsx+++ b/apps/web/app/proxy/[domain]/[key]/page.tsx@@ -30,8 +30,7 @@ export async function generateMetadata({title: unescape(data.title || ""),description: unescape(data.description || ""),image: data.image,- // TODO replace this with value from DB- video: "https://static.mux.com/videos/sandwich.mp4",+ video: data.video,icons: `${GOOGLE_FAVICON_URL}${apexDomain}`,noIndex: true,});diff --git a/apps/web/lib/planetscale.ts b/apps/web/lib/planetscale.tsindex 25d92ace26..1912c9c610 100644--- a/apps/web/lib/planetscale.ts+++ b/apps/web/lib/planetscale.ts@@ -64,6 +64,7 @@ export const getLinkViaEdge = async (domain: string, key: string) => {title: string;description: string;image: string;+ video: string;rewrite: number;password: string | null;expiresAt: string | null;diff --git a/apps/web/lib/upstash.ts b/apps/web/lib/upstash.tsdeleted file mode 100644index 48a9c23337..0000000000--- a/apps/web/lib/upstash.ts+++ /dev/null@@ -1,99 +0,0 @@-import { getDomainWithoutWWW, isIframeable } from "@dub/utils";-import { Ratelimit } from "@upstash/ratelimit";-import { Redis } from "@upstash/redis";-import { LinkProps, RedisLinkProps } from "./types";--// Initiate Redis instance by connecting to REST URL-export const redis = new Redis({- url: process.env.UPSTASH_REDIS_REST_URL || "",- token: process.env.UPSTASH_REDIS_REST_TOKEN || "",-});--export const ratelimitRedis = new Redis({- url:- process.env.RATELIMIT_UPSTASH_REDIS_REST_URL ||- process.env.UPSTASH_REDIS_REST_URL ||- "",- token:- process.env.RATELIMIT_UPSTASH_REDIS_REST_TOKEN ||- process.env.UPSTASH_REDIS_REST_TOKEN ||- "",-});--// Create a new ratelimiter, that allows 10 requests per 10 seconds by default-export const ratelimit = (- requests: number = 10,- seconds:- | `${number} ms`- | `${number} s`- | `${number} m`- | `${number} h`- | `${number} d` = "10 s",-) => {- return new Ratelimit({- redis: ratelimitRedis,- limiter: Ratelimit.slidingWindow(requests, seconds),- analytics: true,- prefix: "dub",- });-};--/**- * Recording metatags that were generated via `/api/metatags`- * If there's an error, it will be logged to a separate redis list for debugging- **/-export async function recordMetatags(url: string, error: boolean) {- if (url === "https://github.com/dubinc/dub") {- // don't log metatag generation for default URL- return null;- }-- if (error) {- return await ratelimitRedis.zincrby("metatags-error-zset", 1, url);- }-- const domain = getDomainWithoutWWW(url);- return await ratelimitRedis.zincrby("metatags-zset", 1, domain);-}--export async function formatRedisLink(- link: LinkProps,-): Promise<RedisLinkProps> {- const {- id,- domain,- url,- trackConversion,- password,- proxy,- rewrite,- expiresAt,- expiredUrl,- ios,- android,- geo,- doIndex,- projectId,- } = link;- const hasPassword = password && password.length > 0 ? true : false;-- return {- id,- ...(url && { url }), // on free plans you cannot set a root domain redirect, hence URL is undefined- ...(trackConversion && { trackConversion: true }),- ...(hasPassword && { password: true }),- ...(proxy && { proxy: true }),- ...(url &&- rewrite && {- rewrite: true,- iframeable: await isIframeable({ url, requestDomain: domain }),- }),- ...(expiresAt && { expiresAt: new Date(expiresAt) }),- ...(expiredUrl && { expiredUrl }),- ...(ios && { ios }),- ...(android && { android }),- ...(geo && { geo: geo as object }),- ...(projectId && { projectId }), // projectId can be undefined for anonymous links- ...(doIndex && { doIndex: true }),- };-}diff --git a/apps/web/lib/upstash/format-redis-link.ts b/apps/web/lib/upstash/format-redis-link.tsnew file mode 100644index 0000000000..f2406e3849--- /dev/null+++ b/apps/web/lib/upstash/format-redis-link.ts@@ -0,0 +1,44 @@+import { isIframeable } from "@dub/utils";+import { LinkProps, RedisLinkProps } from "../types";++export async function formatRedisLink(+ link: LinkProps,+): Promise<RedisLinkProps> {+ const {+ id,+ domain,+ url,+ trackConversion,+ password,+ proxy,+ rewrite,+ expiresAt,+ expiredUrl,+ ios,+ android,+ geo,+ doIndex,+ projectId,+ } = link;+ const hasPassword = password && password.length > 0 ? true : false;++ return {+ id,+ ...(url && { url }), // on free plans you cannot set a root domain redirect, hence URL is undefined+ ...(trackConversion && { trackConversion: true }),+ ...(hasPassword && { password: true }),+ ...(proxy && { proxy: true }),+ ...(url &&+ rewrite && {+ rewrite: true,+ iframeable: await isIframeable({ url, requestDomain: domain }),+ }),+ ...(expiresAt && { expiresAt: new Date(expiresAt) }),+ ...(expiredUrl && { expiredUrl }),+ ...(ios && { ios }),+ ...(android && { android }),+ ...(geo && { geo: geo as object }),+ ...(projectId && { projectId }), // projectId can be undefined for anonymous links+ ...(doIndex && { doIndex: true }),+ };+}diff --git a/apps/web/lib/upstash/index.ts b/apps/web/lib/upstash/index.tsnew file mode 100644index 0000000000..a18b3b3d16--- /dev/null+++ b/apps/web/lib/upstash/index.ts@@ -0,0 +1,10 @@+export * from "./format-redis-link";+export * from "./ratelimit";+export * from "./record-metatags";+import { Redis } from "@upstash/redis";++// Initiate Redis instance by connecting to REST URL+export const redis = new Redis({+ url: process.env.UPSTASH_REDIS_REST_URL || "",+ token: process.env.UPSTASH_REDIS_REST_TOKEN || "",+});diff --git a/apps/web/lib/upstash/ratelimit.ts b/apps/web/lib/upstash/ratelimit.tsnew file mode 100644index 0000000000..5676c4afe4--- /dev/null+++ b/apps/web/lib/upstash/ratelimit.ts@@ -0,0 +1,31 @@+import { Ratelimit } from "@upstash/ratelimit";+import { Redis } from "@upstash/redis";++export const ratelimitRedis = new Redis({+ url:+ process.env.RATELIMIT_UPSTASH_REDIS_REST_URL ||+ process.env.UPSTASH_REDIS_REST_URL ||+ "",+ token:+ process.env.RATELIMIT_UPSTASH_REDIS_REST_TOKEN ||+ process.env.UPSTASH_REDIS_REST_TOKEN ||+ "",+});++// Create a new ratelimiter, that allows 10 requests per 10 seconds by default+export const ratelimit = (+ requests: number = 10,+ seconds:+ | `${number} ms`+ | `${number} s`+ | `${number} m`+ | `${number} h`+ | `${number} d` = "10 s",+) => {+ return new Ratelimit({+ redis: ratelimitRedis,+ limiter: Ratelimit.slidingWindow(requests, seconds),+ analytics: true,+ prefix: "dub",+ });+};diff --git a/apps/web/lib/upstash/record-metatags.ts b/apps/web/lib/upstash/record-metatags.tsnew file mode 100644index 0000000000..edba8b49f7--- /dev/null+++ b/apps/web/lib/upstash/record-metatags.ts@@ -0,0 +1,20 @@+import { getDomainWithoutWWW } from "@dub/utils";+import { ratelimitRedis } from "./ratelimit";++/**+ * Recording metatags that were generated via `/api/metatags`+ * If there's an error, it will be logged to a separate redis list for debugging+ **/+export async function recordMetatags(url: string, error: boolean) {+ if (url === "https://github.com/dubinc/dub") {+ // don't log metatag generation for default URL+ return null;+ }++ if (error) {+ return await ratelimitRedis.zincrby("metatags-error-zset", 1, url);+ }++ const domain = getDomainWithoutWWW(url);+ return await ratelimitRedis.zincrby("metatags-zset", 1, domain);+}diff --git a/apps/web/prisma/schema/link.prisma b/apps/web/prisma/schema/link.prismaindex d5022c1674..4340411f43 100644--- a/apps/web/prisma/schema/link.prisma+++ b/apps/web/prisma/schema/link.prisma@@ -15,6 +15,7 @@ model Link {title String? // OG title for the link (e.g. Dub.co - Open-Source Link Management Infrastructure)description String? @db.VarChar(280) // OG description for the link (e.g. An open-source link management tool for modern marketing teams to create, share, and track short links.)image String? @db.LongText // OG image for the link (e.g. https://d.to/og)+ video String? @db.LongText // OG video for the link// UTM parametersutm_source String? // UTM source for the link (e.g. youtube.com)