The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Finalized advanced options, improved domain suggestion heuristic, increased body limit for API routes
Steven Tey856ac86d8378
diff --git a/components/app/modals/add-edit-link-modal/expiration-section.tsx b/components/app/modals/add-edit-link-modal/expiration-section.tsxindex bbe07b5f14..22655a1b54 100644--- a/components/app/modals/add-edit-link-modal/expiration-section.tsx+++ b/components/app/modals/add-edit-link-modal/expiration-section.tsx@@ -1,56 +1,56 @@-import { Dispatch, SetStateAction } from "react";-import { X, QuestionCircle } from "@/components/shared/icons";+import { Dispatch, SetStateAction, useEffect, useState } from "react";import { LinkProps } from "@/lib/types";import { getDateTimeLocal } from "@/lib/utils";-import Tooltip from "@/components/shared/tooltip";+import Switch from "@/components/shared/switch";+import { motion } from "framer-motion";+import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/constants";export default function ExpirationSection({+ props,data,setData,}: {+ props: LinkProps;data: LinkProps;setData: Dispatch<SetStateAction<LinkProps>>;}) {const { expiresAt } = data;+ const [enabled, setEnabled] = useState(!!expiresAt);+ useEffect(() => {+ if (enabled) {+ // if enabling, add previous expiration date if exists+ setData({+ ...data,+ expiresAt: props?.expiresAt || expiresAt,+ });+ } else {+ // if disabling, remove expiration date+ setData({ ...data, expiresAt: null });+ }+ }, [enabled]);+return (- <div>- <div className="flex items-center">- <label- htmlFor="expiresAt"- className="text-sm font-medium text-gray-900"- >- Expiration Date- </label>- <Tooltip- content="Automatically expires your link at a given date and time. Your link- will be disabled but the data will still be kept."- >- <div className="flex h-4 w-8 justify-center">- <QuestionCircle className="h-4 w-4 text-gray-600" />- </div>- </Tooltip>- </div>- <div className="mt-1 flex space-x-2">- <input- type="datetime-local"- id="expiresAt"- name="expiresAt"- min={getDateTimeLocal()}- value={expiresAt ? getDateTimeLocal(expiresAt) : ""}- step="60" // need to add step to prevent weird date bug (https://stackoverflow.com/q/19284193/10639526)- onChange={(e) => {- setData({ ...data, expiresAt: new Date(e.target.value) });- }}- className="flex w-full items-center justify-center space-x-2 rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-500 shadow-sm transition-all hover:border-gray-800 focus:border-gray-800 focus:outline-none focus:ring-gray-500"- />- <button- onClick={() => setData({ ...data, expiresAt: null })}- type="button"- className="group flex h-10 w-10 items-center justify-center rounded-md border text-gray-500 transition-all hover:border-gray-800 hover:text-gray-800 focus:outline-none"- >- <X className="h-4 w-4 text-gray-400 transition-all group-hover:text-black" />- </button>+ <div className="border-b border-gray-200 pb-5">+ <div className="flex items-center justify-between">+ <h2 className="text-sm font-medium text-gray-900">Expiration Date</h2>+ <Switch fn={() => setEnabled(!enabled)} checked={enabled} /></div>+ {enabled && (+ <motion.div className="mt-3">+ <input+ type="datetime-local"+ id="expiresAt"+ name="expiresAt"+ min={getDateTimeLocal()}+ value={expiresAt ? getDateTimeLocal(expiresAt) : ""}+ step="60" // need to add step to prevent weird date bug (https://stackoverflow.com/q/19284193/10639526)+ onChange={(e) => {+ setData({ ...data, expiresAt: new Date(e.target.value) });+ }}+ className="flex w-full items-center justify-center space-x-2 rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-500 shadow-sm transition-all hover:border-gray-800 focus:border-gray-800 focus:outline-none focus:ring-gray-500"+ />+ </motion.div>+ )}</div>);}diff --git a/components/app/modals/add-edit-link-modal/index.tsx b/components/app/modals/add-edit-link-modal/index.tsxindex f4ae2fc4c5..52c6a36ff1 100644--- a/components/app/modals/add-edit-link-modal/index.tsx+++ b/components/app/modals/add-edit-link-modal/index.tsx@@ -447,8 +447,8 @@ function AddEditLinkModal({generatingMetatags={generatingMetatags}/><UTMSection {...{ props, data, setData }} />- <PasswordSection {...{ data, setData }} />- <ExpirationSection {...{ data, setData }} />+ <PasswordSection {...{ props, data, setData }} />+ <ExpirationSection {...{ props, data, setData }} /></div><div className="sticky bottom-0 bg-gray-50 px-4 py-8 sm:px-16">diff --git a/components/app/modals/add-edit-link-modal/og-section.tsx b/components/app/modals/add-edit-link-modal/og-section.tsxindex 6db22eda98..019ba065b0 100644--- a/components/app/modals/add-edit-link-modal/og-section.tsx+++ b/components/app/modals/add-edit-link-modal/og-section.tsx@@ -72,13 +72,6 @@ export default function OGSection({checked={customOg}/></div>- {/* <p className="mt-2 block text-sm text-gray-500">- If you use custom social previews,{" "}- <span className="font-semibold text-black">- be sure to set all 3 sections (title, description, image)- </span>- , or the default OG tags of the target URL will be used.- </p> */}{customOg && (<motion.div@@ -159,7 +152,7 @@ export default function OGSection({name="title"id={`title-${randomIdx}`}minRows={3}- maxLength={60}+ maxLength={120}className="block w-full rounded-md border-gray-300 pr-10 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"placeholder="Dub - Open Source Bitly Alternative"value={title || ""}@@ -188,7 +181,7 @@ export default function OGSection({name="description"id={`description-${randomIdx}`}minRows={3}- maxLength={200}+ maxLength={280}className="block w-full rounded-md border-gray-300 pr-10 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"placeholder="Dub is open-source link management tool for modern marketing teams to create, share, and track short links."value={description || ""}diff --git a/components/app/modals/add-edit-link-modal/password-section.tsx b/components/app/modals/add-edit-link-modal/password-section.tsxindex 02a05373d1..045e113301 100644--- a/components/app/modals/add-edit-link-modal/password-section.tsx+++ b/components/app/modals/add-edit-link-modal/password-section.tsx@@ -1,44 +1,59 @@-import { Dispatch, SetStateAction } from "react";+import { Dispatch, SetStateAction, useEffect, useState } from "react";import { LinkProps } from "@/lib/types";-import Tooltip from "@/components/shared/tooltip";-import { QuestionCircle } from "@/components/shared/icons";+import Switch from "@/components/shared/switch";+import { motion } from "framer-motion";+import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/constants";export default function PasswordSection({+ props,data,setData,}: {+ props: LinkProps;data: LinkProps;setData: Dispatch<SetStateAction<LinkProps>>;}) {const { password } = data;+ const [enabled, setEnabled] = useState(!!password);+ useEffect(() => {+ if (enabled) {+ // if enabling, add previous password if exists+ setData({+ ...data,+ password: props?.password || password,+ });+ } else {+ // if disabling, remove password+ setData({ ...data, password: null });+ }+ }, [enabled]);return (- <div>- <div className="flex items-center">- <label htmlFor="password" className="text-sm font-medium text-gray-900">+ <div className="border-b border-gray-200 pb-5">+ <div className="flex items-center justify-between">+ <h2 className="text-sm font-medium text-gray-900">Password Protection- </label>- <Tooltip- content="Protect your links with a password. Users will need to enter the- password to access the link."- >- <div className="flex h-4 w-8 justify-center">- <QuestionCircle className="h-4 w-4 text-gray-600" />- </div>- </Tooltip>- </div>- <div className="mt-1 flex rounded-md shadow-sm">- <input- name="password"- id="password"- type="password"- className="block w-full rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"- value={password || ""}- onChange={(e) => {- setData({ ...data, password: e.target.value });- }}- aria-invalid="true"- />+ </h2>+ <Switch fn={() => setEnabled(!enabled)} checked={enabled} /></div>+ {enabled && (+ <motion.div+ className="mt-3 flex rounded-md shadow-sm"+ {...FADE_IN_ANIMATION_SETTINGS}+ >+ <input+ name="password"+ id="password"+ type="password"+ className="block w-full rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"+ value={password || ""}+ placeholder="Enter password"+ onChange={(e) => {+ setData({ ...data, password: e.target.value });+ }}+ aria-invalid="true"+ />+ </motion.div>+ )}</div>);}diff --git a/components/app/modals/add-edit-link-modal/utm-section.tsx b/components/app/modals/add-edit-link-modal/utm-section.tsxindex 00403ceb18..1983a21f1a 100644--- a/components/app/modals/add-edit-link-modal/utm-section.tsx+++ b/components/app/modals/add-edit-link-modal/utm-section.tsx@@ -57,7 +57,7 @@ export default function UTMSection({<Switch fn={() => setEnabled(!enabled)} checked={enabled} /></div>{enabled && (- <motion.div className="mt-1 grid gap-2" {...FADE_IN_ANIMATION_SETTINGS}>+ <motion.div className="mt-3 grid gap-2" {...FADE_IN_ANIMATION_SETTINGS}>{paramsMetadata.map(({ display, key, examples }) => (<div key={key} className="relative mt-1 flex rounded-md shadow-sm"><span className="flex w-60 items-center justify-center whitespace-nowrap rounded-l-md border border-r-0 border-gray-300 bg-gray-50 text-sm text-gray-500">diff --git a/components/app/modals/add-project-modal.tsx b/components/app/modals/add-project-modal.tsxindex 9ae7a5ea3e..5676077eed 100644--- a/components/app/modals/add-project-modal.tsx+++ b/components/app/modals/add-project-modal.tsx@@ -13,7 +13,7 @@ import { useDebounce } from "use-debounce";import BlurImage from "@/components/shared/blur-image";import { AlertCircleFill, LoadingDots } from "@/components/shared/icons";import Modal from "@/components/shared/modal";-import { generateSlugFromName } from "@/lib/utils";+import { generateDomainFromName } from "@/lib/utils";function AddProjectModalHelper({showAddProjectModal,@@ -71,7 +71,7 @@ function AddProjectModalHelper({setData((prev) => ({...prev,slug: name.toLowerCase().replaceAll(" ", "-"),- domain: generateSlugFromName(name),+ domain: generateDomainFromName(name),}));}, [name]);diff --git a/lib/api/links.ts b/lib/api/links.tsindex e9d484a480..5a638abfe9 100644--- a/lib/api/links.ts+++ b/lib/api/links.ts@@ -129,6 +129,7 @@ export async function addLink(link: LinkProps) {const { domain, key, url, expiresAt, password, image, customOg } = link;const hasPassword = password && password.length > 0 ? true : false;const exat = expiresAt ? new Date(expiresAt).getTime() / 1000 : null;+ const uploadedImage = image && image.startsWith("data:image") ? true : false;const exists = await checkIfKeyExists(domain, key);if (exists) return null;@@ -140,6 +141,7 @@ export async function addLink(link: LinkProps) {prisma.link.create({data: {...link,+ image: uploadedImage ? undefined : image,utm_source,utm_medium,utm_campaign,@@ -189,6 +191,7 @@ export async function editLink(const hasPassword = password && password.length > 0 ? true : false;const exat = expiresAt ? new Date(expiresAt).getTime() : null;const changedKey = key !== oldKey;+ const uploadedImage = image && image.startsWith("data:image") ? true : false;if (changedKey) {const exists = await checkIfKeyExists(domain, key);@@ -204,6 +207,7 @@ export async function editLink(},data: {...link,+ image: uploadedImage ? undefined : image,utm_source,utm_medium,utm_campaign,@@ -212,14 +216,16 @@ export async function editLink(},}),// only upload image to cloudinary if customOg is true and there's an image- customOg &&- image &&- cloudinary.v2.uploader.upload(image, {- public_id: key,- folder: domain,- overwrite: true,- invalidate: true,- }),+ customOg && image+ ? cloudinary.v2.uploader.upload(image, {+ public_id: key,+ folder: domain,+ overwrite: true,+ invalidate: true,+ })+ : cloudinary.v2.uploader.destroy(`${domain}/${key}`, {+ invalidate: true,+ }),redis.set(`${domain}:${key}`,{diff --git a/lib/utils.ts b/lib/utils.tsindex f9a0dee371..3c8ac7cd40 100644--- a/lib/utils.ts+++ b/lib/utils.ts@@ -97,7 +97,7 @@ export const getDateTimeLocal = (timestamp?: Date): string => {.join(":");};-export const generateSlugFromName = (name: string) => {+export const generateDomainFromName = (name: string) => {const normalizedName = name.toLowerCase().replaceAll(" ", "-");if (normalizedName.length < 3) {return "";@@ -111,20 +111,11 @@ export const generateSlugFromName = (name: string) => {return `${devowel.slice(0, -2)}.${devowel.slice(-2)}`;}- const acronym = normalizedName- .split("-")- .map((word) => word[0])- .join("");-- if (acronym.length >= 3 && ccTLDs.has(acronym.slice(-2))) {- return `${acronym.slice(0, -2)}.${acronym.slice(-2)}`;- }-- const shortestString = [normalizedName, devowel, acronym].reduce((a, b) =>+ const shortestString = [normalizedName, devowel].reduce((a, b) =>a.length < b.length ? a : b,);- return `${shortestString}.sh`;+ return `${shortestString}.to`;};export const validDomainRegex = new RegExp(diff --git a/pages/api/links/[key]/index.ts b/pages/api/links/[key]/index.tsindex 0e69cde69c..f7b0cc9227 100644--- a/pages/api/links/[key]/index.ts+++ b/pages/api/links/[key]/index.ts@@ -4,6 +4,14 @@ import { Session, withUserAuth } from "@/lib/auth";import prisma from "@/lib/prisma";import { getBlackListedDomains, getDomainWithoutWWW } from "@/lib/utils";+export const config = {+ api: {+ bodyParser: {+ sizeLimit: "1500kb",+ },+ },+};+const domain = "dub.sh";export default withUserAuth(diff --git a/pages/api/links/index.ts b/pages/api/links/index.tsindex a675256902..505e57ec37 100644--- a/pages/api/links/index.ts+++ b/pages/api/links/index.ts@@ -4,8 +4,15 @@ import { Session, withUserAuth } from "@/lib/auth";import { getBlackListedDomains, getDomainWithoutWWW } from "@/lib/utils";import { log } from "@/lib/utils";-// This is a special route for retrieving and creating custom dub.sh links.+export const config = {+ api: {+ bodyParser: {+ sizeLimit: "1500kb",+ },+ },+};+// This is a special route for retrieving and creating custom dub.sh links.export default withUserAuth(async (req: NextApiRequest, res: NextApiResponse, session: Session) => {// GET /api/links – get all dub.sh links created by the userdiff --git a/pages/api/projects/[slug]/domains/[domain]/links/[key]/index.ts b/pages/api/projects/[slug]/domains/[domain]/links/[key]/index.tsindex a6a3404f93..8d845f4f5e 100644--- a/pages/api/projects/[slug]/domains/[domain]/links/[key]/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/links/[key]/index.ts@@ -2,6 +2,14 @@ import { NextApiRequest, NextApiResponse } from "next";import { deleteLink, editLink } from "@/lib/api/links";import { withProjectAuth } from "@/lib/auth";+export const config = {+ api: {+ bodyParser: {+ sizeLimit: "1500kb",+ },+ },+};+export default withProjectAuth(async (req: NextApiRequest, res: NextApiResponse, _, session) => {const {diff --git a/pages/api/projects/[slug]/domains/[domain]/links/index.ts b/pages/api/projects/[slug]/domains/[domain]/links/index.tsindex 374c125fc5..b411ee5f60 100644--- a/pages/api/projects/[slug]/domains/[domain]/links/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/links/index.ts@@ -2,6 +2,14 @@ import { NextApiRequest, NextApiResponse } from "next";import { addLink, getLinksForProject } from "@/lib/api/links";import { withProjectAuth } from "@/lib/auth";+export const config = {+ api: {+ bodyParser: {+ sizeLimit: "1500kb",+ },+ },+};+export default withProjectAuth(async (req: NextApiRequest, res: NextApiResponse, _, session) => {// GET /api/projects/[slug]/domains/[domain]/links - Get all links for a project