The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix encode/decodeURIComponent for stats pages
Steven Tey588dcf419896
diff --git a/app/ui/stats/index.tsx b/app/ui/stats/index.tsxindex 3125be1982..eff8280be4 100644--- a/app/ui/stats/index.tsx+++ b/app/ui/stats/index.tsx@@ -55,6 +55,8 @@ export default function Stats({const pathname = usePathname();const router = useRouter();+ // Note: interestingly, useParams() returns `key` as encoded, unlike useRouter().query+ // so we don't need to do encodeURIComponent() herelet {slug,domain: domainSlug,@@ -81,25 +83,25 @@ export default function Stats({// Project link page, e.g. app.dub.sh/dub/dub.sh/githubif (slug && domainSlug && key) {return {- basePath: `/${slug}/${domainSlug}/${encodeURIComponent(key)}`,+ basePath: `/${slug}/${domainSlug}/${key}`,domain: domainSlug,- endpoint: `/api/links/${encodeURIComponent(key)}/stats`,+ endpoint: `/api/links/${key}/stats`,};// Generic Dub.sh link page, e.g. app.dub.sh/links/steven} else if (key && pathname?.startsWith("/links")) {return {- basePath: `/links/${encodeURIComponent(key)}`,+ basePath: `/links/${key}`,domain: "dub.sh",- endpoint: `/api/links/${encodeURIComponent(key)}/stats`,+ endpoint: `/api/links/${key}/stats`,};}// Public stats page, e.g. dub.sh/stats/github, stey.me/stats/weathergptreturn {- basePath: `/stats/${encodeURIComponent(key)}`,+ basePath: `/stats/${key}`,domain: staticDomain,- endpoint: `/api/edge/links/${encodeURIComponent(key)}/stats`,+ endpoint: `/api/edge/links/${key}/stats`,};}, [slug, key, pathname]);@@ -111,7 +113,7 @@ export default function Stats({endpoint, // endpoint for the API (e.g. /api/edge/links/[key]/stats)queryString, // query string for the API (e.g. ?interval=24h&domain=dub.sh, ?interval=24h, etc.)interval, // time interval (e.g. 24h, 7d, 30d, etc.)- key, // link key (e.g. github, weathergpt, etc.)+ key: decodeURIComponent(key), // link key (e.g. github, weathergpt, etc.)modal, // whether or not this is a modal}}>diff --git a/app/ui/stats/toggle.tsx b/app/ui/stats/toggle.tsxindex 8377329098..2571752d5e 100644--- a/app/ui/stats/toggle.tsx+++ b/app/ui/stats/toggle.tsx@@ -153,7 +153,9 @@ const SharePopover = () => {if (res.status === 200) {mutate(`${endpoint}${queryString}`);!publicStats &&- navigator.clipboard.writeText(`https://${domain}/stats/${key}`);+ navigator.clipboard.writeText(+ `https://${domain}/stats/${encodeURIComponent(key)}`,+ );// artificial delay to sync toast with the switch changeawait new Promise((r) => setTimeout(r, 200));}@@ -200,14 +202,14 @@ const SharePopover = () => {<div className="divide-x-200 mt-2 flex items-center justify-between divide-x overflow-hidden rounded-md border border-gray-200 bg-gray-100"><div className="overflow-scroll pl-2 scrollbar-hide"><p className="whitespace-nowrap text-gray-600">- https://{domain}/stats/{key}+ https://{domain}/stats/{encodeURIComponent(key)}</p></div><buttonclassName="h-8 flex-none border-l bg-white px-2 hover:bg-gray-50 active:bg-gray-100"onClick={() => {navigator.clipboard.writeText(- `https://${domain}/stats/${key}`,+ `https://${domain}/stats/${encodeURIComponent(key)}`,);setCopied(true);toast.success("Copied to clipboard");diff --git a/components/stats/toggle.tsx b/components/stats/toggle.tsxindex 2cd0b0527c..e55127e0bf 100644--- a/components/stats/toggle.tsx+++ b/components/stats/toggle.tsx@@ -178,7 +178,9 @@ const SharePopover = () => {if (res.status === 200) {mutate(`${endpoint}${queryString}`);!publicStats &&- navigator.clipboard.writeText(`https://${domain}/stats/${key}`);+ navigator.clipboard.writeText(+ `https://${domain}/stats/${encodeURIComponent(key)}`,+ );// artificial delay to sync toast with the switch changeawait new Promise((r) => setTimeout(r, 200));}@@ -225,14 +227,14 @@ const SharePopover = () => {<div className="divide-x-200 mt-2 flex items-center justify-between divide-x overflow-hidden rounded-md border border-gray-200 bg-gray-100"><div className="overflow-scroll pl-2 scrollbar-hide"><p className="whitespace-nowrap text-gray-600">- https://{domain}/stats/{key}+ https://{domain}/stats/{encodeURIComponent(key)}</p></div><buttonclassName="h-8 flex-none border-l bg-white px-2 hover:bg-gray-50 active:bg-gray-100"onClick={() => {navigator.clipboard.writeText(- `https://${domain}/stats/${key}`,+ `https://${domain}/stats/${encodeURIComponent(key)}`,);setCopied(true);toast.success("Copied to clipboard");diff --git a/lib/planetscale.ts b/lib/planetscale.tsindex da8df993ac..39cffc785f 100644--- a/lib/planetscale.ts+++ b/lib/planetscale.ts@@ -29,7 +29,7 @@ export const getLinkViaEdge = async (domain: string, key: string) => {const { rows } =(await conn.execute("SELECT `key`, url, proxy, title, description, image, clicks, userId, publicStats FROM Link WHERE domain = ? AND `key` = ?",- [domain, key],+ [domain, decodeURIComponent(key)], // we need to make sure that the key is always decoded (cause that's how we store it in MySQL))) || {};return rows && Array.isArray(rows) && rows.length > 0diff --git a/lib/stats.ts b/lib/stats.tsindex 48c50cd289..ec0d2a921f 100644--- a/lib/stats.ts+++ b/lib/stats.ts@@ -121,6 +121,8 @@ export const getStats = async ({return null;}+ // Note: we're doing decodeURIComponent here because that's how we store it in MySQL and Tinybird+if (!VALID_TINYBIRD_ENDPOINTS.has(endpoint)) {return null;}@@ -133,7 +135,7 @@ export const getStats = async ({if (endpoint === "clicks" && key !== "_root" && !interval && conn) {const response = await conn.execute("SELECT clicks FROM Link WHERE domain = ? AND `key` = ?",- [domain, key],+ [domain, decodeURIComponent(key)],);try {const clicks = response.rows[0]["clicks"];@@ -147,7 +149,7 @@ export const getStats = async ({`https://api.us-east.tinybird.co/v0/pipes/${endpoint}.json`,);url.searchParams.append("domain", domain);- url.searchParams.append("key", key);+ url.searchParams.append("key", decodeURIComponent(key));if (interval) {url.searchParams.append(