The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
converted urlHostname to apexDomain
Steven Tey58bc24bd52e4
diff --git a/components/app/link-card.tsx b/components/app/link-card.tsxindex fd3ab1f461..0a48debd4c 100644--- a/components/app/link-card.tsx+++ b/components/app/link-card.tsx@@ -8,7 +8,13 @@ import Tooltip, { TooltipContent } from "@/components/shared/tooltip";import useProject from "@/lib/swr/use-project";import useUsage from "@/lib/swr/use-usage";import { LinkProps } from "@/lib/types";-import { fetcher, linkConstructor, nFormatter, timeAgo } from "@/lib/utils";+import {+ fetcher,+ getApexDomain,+ linkConstructor,+ nFormatter,+ timeAgo,+} from "@/lib/utils";import { useAddEditLinkModal } from "./modals/add-edit-link-modal";import { useDeleteLinkModal } from "./modals/delete-link-modal";import { useLinkQRModal } from "./modals/link-qr-modal";@@ -16,7 +22,7 @@ import { useLinkQRModal } from "./modals/link-qr-modal";export default function LinkCard({ props }: { props: LinkProps }) {const { key, url, title, timestamp } = props;- const urlHostname = url ? new URL(url).hostname : "";+ const apexDomain = getApexDomain(url);const router = useRouter();const { slug } = router.query as { slug: string };@@ -52,8 +58,8 @@ export default function LinkCard({ props }: { props: LinkProps }) {<li className="flex flex-col sm:flex-row justify-between items-start sm:items-center space-y-5 sm:space-y-0 bg-white p-4 rounded-lg shadow hover:shadow-md transition-all"><div className="relative flex items-center space-x-4"><BlurImage- src={`https://www.google.com/s2/favicons?sz=64&domain_url=${urlHostname}`}- alt={urlHostname}+ src={`https://www.google.com/s2/favicons?sz=64&domain_url=${apexDomain}`}+ alt={apexDomain}className="w-10 h-10 rounded-full"width={20}height={20}diff --git a/components/app/modals/add-edit-link-modal.tsx b/components/app/modals/add-edit-link-modal.tsxindex f0ba31bf20..75c8706127 100644--- a/components/app/modals/add-edit-link-modal.tsx+++ b/components/app/modals/add-edit-link-modal.tsx@@ -23,7 +23,7 @@ import Tooltip, { TooltipContent } from "@/components/shared/tooltip";import useProject from "@/lib/swr/use-project";import useUsage from "@/lib/swr/use-usage";import { LinkProps } from "@/lib/types";-import { linkConstructor } from "@/lib/utils";+import { getApexDomain, linkConstructor } from "@/lib/utils";function AddEditLinkModal({showAddEditLinkModal,@@ -55,10 +55,10 @@ function AddEditLinkModal({const heroProps = useMemo(() => {if (props?.url) {- const urlHostname = new URL(props.url).hostname;+ const apexDomain = getApexDomain(props.url);return {- avatar: `https://www.google.com/s2/favicons?sz=64&domain_url=${urlHostname}`,- alt: urlHostname,+ avatar: `https://www.google.com/s2/favicons?sz=64&domain_url=${apexDomain}`,+ alt: apexDomain,copy: `Edit ${linkConstructor({key: props.key,domain,diff --git a/components/app/modals/delete-link-modal.tsx b/components/app/modals/delete-link-modal.tsxindex 5aaedb9eb4..dfbe1bc47a 100644--- a/components/app/modals/delete-link-modal.tsx+++ b/components/app/modals/delete-link-modal.tsx@@ -13,7 +13,7 @@ import LoadingDots from "@/components/shared/icons/loading-dots";import Modal from "@/components/shared/modal";import useProject from "@/lib/swr/use-project";import { LinkProps } from "@/lib/types";-import { linkConstructor } from "@/lib/utils";+import { getApexDomain, linkConstructor } from "@/lib/utils";function DeleteLinkModal({showDeleteLinkModal,@@ -28,7 +28,7 @@ function DeleteLinkModal({const { slug } = router.query;const [deleting, setDeleting] = useState(false);const { project: { domain } = {} } = useProject();- const urlHostname = props?.url ? new URL(props.url).hostname : null;+ const apexDomain = getApexDomain(props.url);const shortlink = useMemo(() => {return linkConstructor({@@ -46,9 +46,9 @@ function DeleteLinkModal({<div className="inline-block w-full sm:max-w-md overflow-hidden align-middle transition-all transform bg-white sm:border sm:border-gray-200 shadow-xl sm:rounded-2xl"><div className="flex flex-col justify-center items-center space-y-3 sm:px-16 px-4 pt-8 py-4 border-b border-gray-200"><BlurImage- src={`https://www.google.com/s2/favicons?sz=64&domain_url=${urlHostname}`}- alt={urlHostname}- className="w-10 h-10 rounded-full border border-gray-200"+ src={`https://www.google.com/s2/favicons?sz=64&domain_url=${apexDomain}`}+ alt={apexDomain}+ className="w-10 h-10 rounded-full"width={20}height={20}/>diff --git a/components/app/modals/link-qr-modal.tsx b/components/app/modals/link-qr-modal.tsxindex 9eaa221d1a..1a570d96f0 100644--- a/components/app/modals/link-qr-modal.tsx+++ b/components/app/modals/link-qr-modal.tsx@@ -16,7 +16,7 @@ import { QRCodeSVG, getQRAsCanvas, getQRAsSVGDataUri } from "@/lib/qr";import useProject from "@/lib/swr/use-project";import useUsage from "@/lib/swr/use-usage";import { SimpleLinkProps } from "@/lib/types";-import { linkConstructor } from "@/lib/utils";+import { getApexDomain, linkConstructor } from "@/lib/utils";function LinkQRModalHelper({showLinkQRModal,@@ -29,12 +29,12 @@ function LinkQRModalHelper({}) {const anchorRef = useRef<HTMLAnchorElement>();const { project: { domain, logo } = {} } = useProject();- const { avatarUrl, urlHostname } = useMemo(() => {+ const { avatarUrl, apexDomain } = useMemo(() => {try {- const urlHostname = new URL(props.url).hostname;+ const apexDomain = getApexDomain(props.url);return {- avatarUrl: `https://www.google.com/s2/favicons?sz=64&domain_url=${urlHostname}`,- urlHostname,+ avatarUrl: `https://www.google.com/s2/favicons?sz=64&domain_url=${apexDomain}`,+ apexDomain,};} catch (e) {return null;@@ -84,8 +84,8 @@ function LinkQRModalHelper({{avatarUrl ? (<BlurImagesrc={avatarUrl}- alt={urlHostname}- className="w-10 h-10 rounded-full border border-gray-200"+ alt={apexDomain}+ className="w-10 h-10 rounded-full"width={40}height={40}/>diff --git a/components/home/link-card.tsx b/components/home/link-card.tsxindex cc6290db99..c07d7d3f36 100644--- a/components/home/link-card.tsx+++ b/components/home/link-card.tsx@@ -10,7 +10,12 @@ import CopyButton from "@/components/shared/copy-button";import { Chart, LoadingDots, QR } from "@/components/shared/icons";import { FRAMER_MOTION_LIST_ITEM_VARIANTS } from "@/lib/constants";import { SimpleLinkProps } from "@/lib/types";-import { fetcher, linkConstructor, nFormatter } from "@/lib/utils";+import {+ fetcher,+ getApexDomain,+ linkConstructor,+ nFormatter,+} from "@/lib/utils";export default function LinkCard({_key: key,@@ -23,7 +28,7 @@ export default function LinkCard({hashes?: SimpleLinkProps[];setHashes?: (hashes: SimpleLinkProps[]) => void;}) {- const urlHostname = new URL(url).hostname;+ const apexDomain = getApexDomain(url);const { data: clicks, isValidating } = useSWR<string>(`/api/edge/links/${key}/clicks`,@@ -119,8 +124,8 @@ export default function LinkCard({className="cursor-grab active:cursor-grabbing flex items-center space-x-3 border border-gray-200 hover:border-black bg-white p-3 max-w-md rounded-md transition-[border-color]"><BlurImage- src={`https://www.google.com/s2/favicons?sz=64&domain_url=${urlHostname}`}- alt={urlHostname}+ src={`https://www.google.com/s2/favicons?sz=64&domain_url=${apexDomain}`}+ alt={apexDomain}className="w-10 h-10 rounded-full pointer-events-none"width={20}height={20}diff --git a/lib/constants.ts b/lib/constants.tsindex f000b3e7f9..90538da592 100644--- a/lib/constants.ts+++ b/lib/constants.ts@@ -309,7 +309,7 @@ export const COUNTRIES: { [key: string]: string } = {XK: "Kosovo",};-export const ccTLDs = [+export const ccTLDs = new Set(["af","ax","al",@@ -582,4 +582,14 @@ export const ccTLDs = ["ye","zm","zw",-];+]);++export const secondLevelDomains = new Set([+ "com",+ "co",+ "net",+ "org",+ "edu",+ "gov",+ "in",+]);diff --git a/lib/utils.ts b/lib/utils.tsindex 5b03b1fb68..d44d7111f3 100644--- a/lib/utils.ts+++ b/lib/utils.ts@@ -1,5 +1,5 @@import ms from "ms";-import { ccTLDs } from "./constants";+import { ccTLDs, secondLevelDomains } from "./constants";interface SWRError extends Error {status: number;@@ -118,12 +118,12 @@ export const generateSlugFromName = (name: string) => {if (normalizedName.length < 3) {return "";}- if (ccTLDs.some((tld) => normalizedName.endsWith(tld))) {+ if (ccTLDs.has(normalizedName.slice(-2))) {return `${normalizedName.slice(0, -2)}.${normalizedName.slice(-2)}`;}// remove vowelsconst devowel = normalizedName.replace(/[aeiou]/g, "");- if (devowel.length >= 3 && ccTLDs.some((tld) => devowel.endsWith(tld))) {+ if (devowel.length >= 3 && ccTLDs.has(devowel.slice(-2))) {return `${devowel.slice(0, -2)}.${devowel.slice(-2)}`;}@@ -132,7 +132,7 @@ export const generateSlugFromName = (name: string) => {.map((word) => word[0]).join("");- if (acronym.length >= 3 && ccTLDs.some((tld) => acronym.endsWith(tld))) {+ if (acronym.length >= 3 && ccTLDs.has(acronym.slice(-2))) {return `${acronym.slice(0, -2)}.${acronym.slice(-2)}`;}@@ -173,3 +173,26 @@ export const getSubdomain = (name: string, apexName: string) => {if (name === apexName) return null;return name.slice(0, name.length - apexName.length - 1);};++export const getApexDomain = (url: string) => {+ let domain;+ try {+ domain = new URL(url).hostname;+ } catch (e) {+ return "";+ }+ const parts = domain.split(".");+ if (parts.length > 2) {+ // if this is a second-level TLD (e.g. co.uk, .com.ua, .org.tt), we need to return the last 3 parts+ if (+ secondLevelDomains.has(parts[parts.length - 2]) &&+ ccTLDs.has(parts[parts.length - 1])+ ) {+ return parts.slice(-3).join(".");+ }+ // otherwise, it's a subdomain (e.g. dub.vercel.app), so we return the last 2 parts+ return parts.slice(-2).join(".");+ }+ // if it's a normal domain (e.g. dub.sh), we return the domain+ return domain;+};