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.

cache allowedHostnames

Kiran K9a1b1d808c7a
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.ts
index 50e5d3e312..3739cf5706 100644
--- a/apps/web/app/api/track/click/route.ts
+++ b/apps/web/app/api/track/click/route.ts
@@ -1,7 +1,9 @@
+import { verifyAnalyticsAllowedHostnames } from "@/lib/analytics/verify-analytics-allowed-hostnames";
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { linkCache } from "@/lib/api/links/cache";
import { clickCache } from "@/lib/api/links/click-cache";
import { parseRequestBody } from "@/lib/api/utils";
+import { workspaceCache } from "@/lib/api/workspace-cache";
import { getLinkWithPartner } from "@/lib/planetscale/get-link-with-partner";
import { recordClick } from "@/lib/tinybird";
import { formatRedisLink } from "@/lib/upstash";
@@ -89,10 +91,24 @@ export const POST = withAxiom(async (req: AxiomRequest) => {
waitUntil(linkCache.set(link as any));
}
- // verifyAnalyticsAllowedHostnames({
- // allowedHostnames: link.allowedHostnames,
- // req,
- // });
+ if (!cachedLink.projectId) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Link not found.",
+ });
+ }
+
+ const allowedHostnames = await workspaceCache.get({
+ id: cachedLink.projectId,
+ key: "allowedHostnames",
+ });
+
+ if (allowedHostnames) {
+ verifyAnalyticsAllowedHostnames({
+ allowedHostnames,
+ req,
+ });
+ }
const finalUrl = url
? isValidUrl(url)
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
index 8f6076a742..74234e77cf 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
@@ -1,6 +1,7 @@
import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { validateAllowedHostnames } from "@/lib/api/validate-allowed-hostnames";
+import { workspaceCache } from "@/lib/api/workspace-cache";
import { prefixWorkspaceId } from "@/lib/api/workspace-id";
import { deleteWorkspace } from "@/lib/api/workspaces";
import { withWorkspace } from "@/lib/auth";
@@ -115,6 +116,15 @@ export const PATCH = withWorkspace(
if (logoUploaded && workspace.logo) {
await storage.delete(workspace.logo.replace(`${R2_URL}/`, ""));
}
+
+ if (validHostnames) {
+ workspaceCache.set({
+ id: workspace.id,
+ data: {
+ allowedHostnames: validHostnames,
+ },
+ });
+ }
})(),
);
diff --git a/apps/web/lib/api/workspace-cache.ts b/apps/web/lib/api/workspace-cache.ts
new file mode 100644
index 0000000000..521b08d26b
--- /dev/null
+++ b/apps/web/lib/api/workspace-cache.ts
@@ -0,0 +1,33 @@
+import { redis } from "@/lib/upstash";
+
+interface CacheProps {
+ allowedHostnames: string[];
+}
+
+class WorkspaceCache {
+ async set({ id, data }: { id: string; data: CacheProps }) {
+ return await redis.hset(this._createKey(id), {
+ ...data,
+ });
+ }
+
+ async get<K extends keyof CacheProps>({
+ id,
+ key,
+ }: {
+ id: string;
+ key: K;
+ }): Promise<CacheProps[K] | null> {
+ return await redis.hget<CacheProps[K]>(this._createKey(id), key);
+ }
+
+ async delete(id: string) {
+ return await redis.del(this._createKey(id));
+ }
+
+ _createKey(id: string) {
+ return `workspace:${id}`;
+ }
+}
+
+export const workspaceCache = new WorkspaceCache();
diff --git a/apps/web/lib/api/workspaces.ts b/apps/web/lib/api/workspaces.ts
index 71de693888..105c09c29b 100644
--- a/apps/web/lib/api/workspaces.ts
+++ b/apps/web/lib/api/workspaces.ts
@@ -13,6 +13,7 @@ import { qstash } from "../cron";
import { cancelSubscription } from "../stripe/cancel-subscription";
import { markDomainAsDeleted } from "./domains";
import { linkCache } from "./links/cache";
+import { workspaceCache } from "./workspace-cache";
export async function deleteWorkspace(
workspace: Pick<WorkspaceProps, "id" | "slug" | "logo" | "stripeId">,
@@ -57,6 +58,8 @@ export async function deleteWorkspace(
queueWorkspaceDeletion({
workspaceId: workspace.id,
}),
+
+ workspaceCache.delete(workspace.id),
]),
);
}