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.

Fix broken public stats page

Steven Tey150b7bb3ff92
diff --git a/app/[domain]/stats/[key]/page.tsx b/app/[domain]/stats/[key]/page.tsx
index 7c2e8ba3bf..81a31c8dd9 100644
--- a/app/[domain]/stats/[key]/page.tsx
+++ b/app/[domain]/stats/[key]/page.tsx
@@ -13,13 +13,18 @@ export async function generateMetadata({
}) {
const data = await getLinkViaEdge(params.domain, params.key);
- if (!data || !data.publicStats) {
+ // if the link is explicitly private (publicStats === false)
+ // or if the link doesn't exist in database (data === undefined) and is not a dub.sh link
+ // (we need to exclude dub.sh public demo links here)
+ if (data?.publicStats === 0 || (params.domain !== "dub.sh" && !data)) {
return;
}
return constructMetadata({
title: `Stats for ${params.domain}/${params.key} - Dub`,
- description: `Stats page for ${params.domain}/${params.key}, which redirects to ${data.url}.`,
+ description: `Stats page for ${params.domain}/${params.key}${
+ data?.url ? `, which redirects to ${data.url}` : ""
+ }.`,
image: `https://${params.domain}/api/og/stats?domain=${params.domain}&key=${params.key}`,
});
}
@@ -40,7 +45,7 @@ export default async function StatsPage({
}) {
const data = await getLinkViaEdge(params.domain, params.key);
- if (!data || !data.publicStats) {
+ if (data?.publicStats === 0 || (params.domain !== "dub.sh" && !data)) {
notFound();
}
diff --git a/lib/planetscale.ts b/lib/planetscale.ts
index 1ccf4d5347..75c9fd645b 100644
--- a/lib/planetscale.ts
+++ b/lib/planetscale.ts
@@ -21,7 +21,7 @@ export const getLinkViaEdge = async (domain: string, key: string) => {
url: string;
clicks: number;
userId: number;
- publicStats: boolean;
+ publicStats: number;
})
: null;
};
diff --git a/pages/api/edge/links/[key]/stats/[endpoint].ts b/pages/api/edge/links/[key]/stats/[endpoint].ts
index b640508a05..7afbffca8b 100644
--- a/pages/api/edge/links/[key]/stats/[endpoint].ts
+++ b/pages/api/edge/links/[key]/stats/[endpoint].ts
@@ -19,8 +19,10 @@ export default async function handler(req: NextRequest) {
// if the link is NOT dub.sh/github (demo link)
if (!(domain === "dub.sh" && key === "github")) {
data = await getLinkViaEdge(domain, key);
- // check if the link is public
- if (!data?.publicStats) {
+ // if the link is explicitly private (publicStats === false)
+ // or if the link doesn't exist in database (data === undefined) and is not a dub.sh link
+ // (we need to exclude dub.sh public demo links here)
+ if (data?.publicStats === 0 || (domain !== "dub.sh" && !data)) {
return new Response(`Stats for this link are not public`, {
status: 403,
});