The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Cache webhook only for link level
Kiran Kfd3976b1e485
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.tsindex 3e4b60d293..988b237f1d 100644--- a/apps/web/app/api/webhooks/[webhookId]/route.ts+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts@@ -4,6 +4,7 @@ import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";import { webhookCache } from "@/lib/webhook/cache";import { transformWebhook } from "@/lib/webhook/transform";+import { isLinkLevelWebhook } from "@/lib/webhook/utils";import { updateWebhookSchema } from "@/lib/zod/schemas/webhooks";import { waitUntil } from "@vercel/functions";import { NextResponse } from "next/server";@@ -52,7 +53,7 @@ export const PATCH = withWorkspace(await parseRequestBody(req),);- const existingWebhook = await prisma.webhook.findFirst({+ const webhookUrlExists = await prisma.webhook.findFirst({where: {projectId: workspace.id,url,@@ -62,7 +63,7 @@ export const PATCH = withWorkspace(},});- if (existingWebhook) {+ if (webhookUrlExists) {throw new DubApiError({code: "conflict",message: "A Webhook with this URL already exists.",@@ -114,7 +115,30 @@ export const PATCH = withWorkspace(},});- waitUntil(webhookCache.set(webhook));+ waitUntil(+ (async () => {+ const existingWebhook = await prisma.webhook.findUniqueOrThrow({+ where: {+ id: webhookId,+ projectId: workspace.id,+ },+ });++ // If the webhook is being changed from link level to workspace level, delete the cache+ if (+ isLinkLevelWebhook(existingWebhook) &&+ !isLinkLevelWebhook(webhook)+ ) {+ await webhookCache.delete(webhookId);+ return;+ }++ // If the webhook is being changed from workspace level to link level, set the cache+ if (isLinkLevelWebhook(webhook)) {+ await webhookCache.set(webhook);+ }+ })(),+ );return NextResponse.json(transformWebhook(webhook));},diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.tsindex f19bea9e40..2c681fbcf1 100644--- a/apps/web/app/api/webhooks/route.ts+++ b/apps/web/app/api/webhooks/route.ts@@ -5,6 +5,7 @@ import { prisma } from "@/lib/prisma";import { webhookCache } from "@/lib/webhook/cache";import { WEBHOOK_ID_PREFIX } from "@/lib/webhook/constants";import { transformWebhook } from "@/lib/webhook/transform";+import { isLinkLevelWebhook } from "@/lib/webhook/utils";import { createWebhookSchema } from "@/lib/zod/schemas/webhooks";import { nanoid } from "@dub/utils";import { waitUntil } from "@vercel/functions";@@ -129,7 +130,7 @@ export const POST = withWorkspace(waitUntil(Promise.allSettled([- webhookCache.set(webhook),+ ...(isLinkLevelWebhook(webhook) ? [webhookCache.set(webhook)] : []),sendEmail({email: session.user.email,diff --git a/apps/web/lib/webhook/utils.ts b/apps/web/lib/webhook/utils.tsnew file mode 100644index 0000000000..b56263bed8--- /dev/null+++ b/apps/web/lib/webhook/utils.ts@@ -0,0 +1,15 @@+import type { Webhook } from "@prisma/client";+import { LINK_LEVEL_WEBHOOK_TRIGGERS } from "./constants";++export const isLinkLevelWebhook = (webhook: Pick<Webhook, "triggers">) => {+ if (!webhook.triggers) {+ return false;+ }++ const triggers =+ webhook.triggers as (typeof LINK_LEVEL_WEBHOOK_TRIGGERS)[number][];++ return triggers.some((trigger) =>+ LINK_LEVEL_WEBHOOK_TRIGGERS.includes(trigger),+ );+};