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.

use query param for edge/stats endpoint

Steven Tey482bff60e5ef
diff --git a/app/ui/stats/index.tsx b/app/ui/stats/index.tsx
index 95c83940ec..7fb18710b8 100644
--- a/app/ui/stats/index.tsx
+++ b/app/ui/stats/index.tsx
@@ -71,14 +71,11 @@ export default function Stats({
const interval = searchParams.get("interval") || "24h";
- const queryString =
- interval || domainSlug
- ? `?${new URLSearchParams({
- ...(slug && { slug }),
- ...(interval && { interval }),
- ...(domainSlug && { domain: domainSlug }),
- }).toString()}`
- : "";
+ const queryString = `?${new URLSearchParams({
+ ...(slug && { slug }),
+ ...(interval && { interval }),
+ domain: staticDomain || domainSlug || "dub.sh",
+ }).toString()}`;
const { basePath, domain, endpoint } = useMemo(() => {
// Project link page, e.g. app.dub.co/dub/dub.sh/github
diff --git a/pages/api/edge/links/[key]/stats/[endpoint].ts b/pages/api/edge/links/[key]/stats/[endpoint].ts
index 1d57aa8072..be8751d9d3 100644
--- a/pages/api/edge/links/[key]/stats/[endpoint].ts
+++ b/pages/api/edge/links/[key]/stats/[endpoint].ts
@@ -12,37 +12,30 @@ export const config = {
export default async function handler(req: NextRequest) {
if (req.method === "GET") {
+ const domain = req.nextUrl.searchParams.get("domain") || "dub.sh";
const key = req.nextUrl.pathname.split("/")[4];
const interval = req.nextUrl.searchParams.get("interval");
const endpoint = req.nextUrl.searchParams.get("endpoint") as string;
- let domain = req.nextUrl.hostname;
- if (isHomeHostname(domain) || domain === "api.dub.sh") {
- domain = "dub.sh";
- }
- if (
- process.env.NODE_ENV !== "development" &&
- domain === "dub.sh" &&
- key === "github"
- ) {
- if (await isBlacklistedReferrer(req.headers.get("referer"))) {
- return new Response("Don't DDoS me pls 🥺", { status: 429 });
- }
- const ip = ipAddress(req) || LOCALHOST_IP;
- const { success } = await ratelimit(
- 15,
- endpoint !== "clicks" ? "1 h" : "10 s",
- ).limit(`${ip}:${domain}:${key}:${endpoint}`);
+ // demo link (dub.sh/github)
+ if (domain === "dub.sh" && key === "github") {
+ // Rate limit in production
+ if (process.env.NODE_ENV !== "development") {
+ if (await isBlacklistedReferrer(req.headers.get("referer"))) {
+ return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ }
+ const ip = ipAddress(req) || LOCALHOST_IP;
+ const { success } = await ratelimit(
+ 15,
+ endpoint !== "clicks" ? "1 h" : "10 s",
+ ).limit(`${ip}:${domain}:${key}:${endpoint}`);
- if (!success) {
- return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ if (!success) {
+ return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ }
}
- }
-
- let data;
- // if the link is NOT dub.sh/github (demo link)
- if (!(domain === "dub.sh" && key === "github")) {
- data = await getLinkViaEdge(domain, key);
+ } else {
+ const data = await getLinkViaEdge(domain, key);
// 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)