The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Update webhook cache handling
Kiran Ke8edfdf7447c
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.tsindex d1044b14d4..7942f4d702 100644--- a/apps/web/app/api/webhooks/[webhookId]/route.ts+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts@@ -104,6 +104,14 @@ export const PATCH = withWorkspace(},});+ // Cache the existing webhook before updating it+ const existingWebhook = await prisma.webhook.findUniqueOrThrow({+ where: {+ id: webhookId,+ projectId: workspace.id,+ },+ });+const webhook = await prisma.webhook.update({where: {id: webhookId,@@ -139,13 +147,6 @@ export const PATCH = withWorkspace(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 cacheif (isLinkLevelWebhook(existingWebhook) &&diff --git a/apps/web/lib/webhook/cache.ts b/apps/web/lib/webhook/cache.tsindex 93959dabb1..bd65b57c1d 100644--- a/apps/web/lib/webhook/cache.ts+++ b/apps/web/lib/webhook/cache.ts@@ -1,36 +1,34 @@import { redis } from "@/lib/upstash";import { WebhookCacheProps } from "../types";-import { WEBHOOK_REDIS_KEY } from "./constants";+import { isLinkLevelWebhook } from "./utils";++const WEBHOOK_CACHE_KEY_PREFIX = "webhook";class WebhookCache {async set(webhook: WebhookCacheProps) {- return await redis.hset(WEBHOOK_REDIS_KEY, {- [webhook.id]: this.format(webhook),- });- }+ if (!isLinkLevelWebhook(webhook)) {+ return;+ }- async get(webhookId: string) {- return await redis.hget<WebhookCacheProps>(WEBHOOK_REDIS_KEY, webhookId);+ return await redis.set(+ this._getCacheKey(webhook.id),+ JSON.stringify(this._format(webhook)),+ );}async mget(webhookIds: string[]) {- const webhooks = await redis.hmget<Record<string, WebhookCacheProps>>(- WEBHOOK_REDIS_KEY,- ...webhookIds,+ const webhooks = await redis.mget<WebhookCacheProps[]>(+ webhookIds.map(this._getCacheKey),);- if (!webhooks) {- return [];- }-- return Object.values(webhooks);+ return webhooks.filter(Boolean);}async delete(webhookId: string) {- return await redis.hdel(WEBHOOK_REDIS_KEY, webhookId);+ return await redis.del(this._getCacheKey(webhookId));}- format(webhook: WebhookCacheProps) {+ _format(webhook: WebhookCacheProps) {return {id: webhook.id,url: webhook.url,@@ -39,6 +37,10 @@ class WebhookCache {...(webhook.disabledAt && { disabledAt: webhook.disabledAt }),};}++ _getCacheKey(webhookId: string) {+ return `${WEBHOOK_CACHE_KEY_PREFIX}:${webhookId}`;+ }}export const webhookCache = new WebhookCache();diff --git a/apps/web/lib/webhook/failure.ts b/apps/web/lib/webhook/failure.tsindex 61ed9cbfb7..b4f182eb37 100644--- a/apps/web/lib/webhook/failure.ts+++ b/apps/web/lib/webhook/failure.ts@@ -33,22 +33,20 @@ export const handleWebhookFailure = async (webhookId: string) => {webhook.consecutiveFailures >= WEBHOOK_FAILURE_NOTIFY_THRESHOLD;if (failureThresholdReached) {- const disabledAt = new Date();-// Disable the webhook- await prisma.webhook.update({+ const updatedWebhook = await prisma.webhook.update({where: { id: webhookId },data: {- disabledAt,+ disabledAt: new Date(),},});await Promise.allSettled([// Notify the user- sendFailureNotification(webhook),+ sendFailureNotification(updatedWebhook),// Update the webhook cache- webhookCache.set({ ...webhook, disabledAt }),+ webhookCache.set(updatedWebhook),// Update the project webhookEnabled flagupdateWebhookStatusForWorkspace({diff --git a/apps/web/ui/modals/delete-webhook-modal.tsx b/apps/web/ui/modals/delete-webhook-modal.tsxindex e11cfd8355..e7c8a398f1 100644--- a/apps/web/ui/modals/delete-webhook-modal.tsx+++ b/apps/web/ui/modals/delete-webhook-modal.tsx@@ -10,6 +10,7 @@ import {useState,} from "react";import { toast } from "sonner";+import { mutate } from "swr";function DeleteWebhookModal({showDeleteWebhookModal,@@ -44,6 +45,7 @@ function DeleteWebhookModal({}setShowDeleteWebhookModal(false);+ mutate(`/api/webhooks?workspaceId=${workspaceId}`);router.push(`/${workspaceSlug}/settings/webhooks`);};