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.

add waitUntil to updateLink and deleteLink

Steven Teyeff310c0326f
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index ff7e072f38..0709564ca4 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -10,6 +10,7 @@ import { withWorkspace } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { NewLinkProps } from "@/lib/types";
import { updateLinkBodySchema } from "@/lib/zod/schemas";
+import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
// GET /api/links/[linkId] – get a link
@@ -129,10 +130,10 @@ export const PUT = PATCH;
// DELETE /api/links/[linkId] – delete a link
export const DELETE = withWorkspace(async ({ headers, link }) => {
- await deleteLink(link!.id);
+ waitUntil(deleteLink(link!.id));
return NextResponse.json(
- { id: link?.id },
+ { id: link!.id },
{
headers,
},
diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.ts
index e96e934856..0ac5ba5331 100644
--- a/apps/web/lib/api/links/update-link.ts
+++ b/apps/web/lib/api/links/update-link.ts
@@ -5,6 +5,7 @@ import { LinkProps, ProcessedLinkProps } from "@/lib/types";
import { formatRedisLink, redis } from "@/lib/upstash";
import { SHORT_DOMAIN, getParamsFromURL, truncate } from "@dub/utils";
import { Prisma } from "@prisma/client";
+import { waitUntil } from "@vercel/functions";
import { combineTagIds, transformLink } from "./utils";
export async function updateLink({
@@ -49,14 +50,6 @@ export async function updateLink({
const combinedTagIds = combineTagIds({ tagId, tagIds });
- if (proxy && image && !isStored(image)) {
- // only upload image if proxy is true and image is not stored in R2
- await storage.upload(`images/${id}`, image, {
- width: 1200,
- height: 630,
- });
- }
-
const response = await prisma.link.update({
where: {
id,
@@ -121,24 +114,34 @@ export async function updateLink({
},
});
- await Promise.all([
- // record link in Redis
- redis.hset(updatedLink.domain.toLowerCase(), {
- [updatedLink.key.toLowerCase()]: await formatRedisLink(response),
- }),
- // record link in Tinybird
- recordLink({
- link: {
- ...response,
- tags: response.tags.map(({ tag }) => ({
- tagId: tag.id,
- })),
- },
- }),
- // if key is changed: delete the old key in Redis
- (changedDomain || changedKey) &&
- redis.hdel(oldDomain.toLowerCase(), oldKey.toLowerCase()),
- ]);
+ waitUntil(
+ Promise.all([
+ // record link in Redis
+ redis.hset(updatedLink.domain.toLowerCase(), {
+ [updatedLink.key.toLowerCase()]: await formatRedisLink(response),
+ }),
+ // record link in Tinybird
+ recordLink({
+ link: {
+ ...response,
+ tags: response.tags.map(({ tag }) => ({
+ tagId: tag.id,
+ })),
+ },
+ }),
+ // if key is changed: delete the old key in Redis
+ (changedDomain || changedKey) &&
+ redis.hdel(oldDomain.toLowerCase(), oldKey.toLowerCase()),
+ // only upload image if proxy is true and image is not stored in R2
+ proxy &&
+ image &&
+ !isStored(image) &&
+ storage.upload(`images/${id}`, image, {
+ width: 1200,
+ height: 630,
+ }),
+ ]),
+ );
return transformLink(response);
}