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 rate limit

Steven Teyd2a0afdd7575
diff --git a/apps/web/app/api/analytics/dashboard/route.ts b/apps/web/app/api/analytics/dashboard/route.ts
index 03c23096bf..710577fa85 100644
--- a/apps/web/app/api/analytics/dashboard/route.ts
+++ b/apps/web/app/api/analytics/dashboard/route.ts
@@ -35,21 +35,6 @@ export const GET = async (req: Request) => {
// if it's a demo link
if (demoLink) {
- // Rate limit in production
- if (process.env.NODE_ENV !== "development") {
- const ip = ipAddress(req);
- const { success } = await ratelimit(
- 15,
- groupBy === "count" ? "10 s" : "1 m",
- ).limit(`demo-analytics:${demoLink.id}:${ip}:${groupBy}`);
-
- if (!success) {
- throw new DubApiError({
- code: "rate_limit_exceeded",
- message: "Don't DDoS me pls 🥺",
- });
- }
- }
link = {
id: demoLink.id,
projectId: DUB_WORKSPACE_ID,
@@ -103,6 +88,26 @@ export const GET = async (req: Request) => {
}
}
+ // Rate limit in production
+ if (process.env.NODE_ENV !== "development") {
+ const ip = ipAddress(req);
+ // for demo links, we rate limit at:
+ // - 15 requests per 10 seconds if groupBy is "count"
+ // - 15 request per minute if groupBy is not "count"
+ // for non-demo links, we rate limit at 10 requests per 10 seconds
+ const { success } = await ratelimit(
+ demoLink ? 15 : 10,
+ !demoLink || groupBy === "count" ? "10 s" : "1 m",
+ ).limit(`analytics-dashboard:${link.id}:${ip}:${groupBy}`);
+
+ if (!success) {
+ throw new DubApiError({
+ code: "rate_limit_exceeded",
+ message: "Don't DDoS me pls 🥺",
+ });
+ }
+ }
+
const response = await getAnalytics({
...parsedParams,
// workspaceId can be undefined (for public links that haven't been claimed/synced to a workspace)