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.

Integrate waitUntil

Steven Teyf3d3549dfa4d
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index f9108e38a3..62740cc1c1 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -2,10 +2,11 @@ import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { isStored, storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird";
-import { ProcessedLinkProps } from "@/lib/types";
+import { LinkProps, ProcessedLinkProps } from "@/lib/types";
import { formatRedisLink, redis } from "@/lib/upstash";
import { APP_DOMAIN_WITH_NGROK, getParamsFromURL, truncate } from "@dub/utils";
import { Prisma } from "@prisma/client";
+import { waitUntil } from "@vercel/functions";
import { combineTagIds, transformLink } from "./utils";
export async function createLink(link: ProcessedLinkProps) {
@@ -76,9 +77,32 @@ export async function createLink(link: ProcessedLinkProps) {
},
});
- const uploadedImageUrl = `${process.env.STORAGE_BASE_URL}/images/${response.id}`;
+ if (process.env.VERCEL === "1") {
+ waitUntil(createLinkEffects({ link, response }));
+ } else {
+ await createLinkEffects({ link, response });
+ }
- await Promise.all([
+ return {
+ ...transformLink(response),
+ // optimistically set the image URL to the uploaded image URL
+ image:
+ proxy && image && !isStored(image)
+ ? `${process.env.STORAGE_BASE_URL}/images/${response.id}`
+ : response.image,
+ };
+}
+
+async function createLinkEffects({
+ link,
+ response,
+}: {
+ link: ProcessedLinkProps;
+ response: LinkProps & { tags: { tag: { id: string } }[] };
+}) {
+ const { image, proxy } = link;
+
+ return await Promise.all([
// record link in Redis
redis.hset(link.domain.toLowerCase(), {
[link.key.toLowerCase()]: await formatRedisLink(response),
@@ -106,7 +130,7 @@ export async function createLink(link: ProcessedLinkProps) {
id: response.id,
},
data: {
- image: uploadedImageUrl,
+ image: `${process.env.STORAGE_BASE_URL}/images/${response.id}`,
},
}),
]
@@ -134,11 +158,4 @@ export async function createLink(link: ProcessedLinkProps) {
},
}),
]);
-
- return {
- ...transformLink(response),
- // optimistically set the image URL to the uploaded image URL
- image:
- proxy && image && !isStored(image) ? uploadedImageUrl : response.image,
- };
}
diff --git a/apps/web/package.json b/apps/web/package.json
index 7a55875feb..6f93dcd8a5 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -33,6 +33,7 @@
"@vercel/analytics": "^1.0.0",
"@vercel/edge": "^0.3.1",
"@vercel/edge-config": "^0.4.1",
+ "@vercel/functions": "^1.0.0",
"@vercel/og": "^0.6.2",
"@vercel/speed-insights": "^1.0.1",
"@visx/axis": "^2.14.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4e79fb6420..2cec152c41 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -97,6 +97,9 @@ importers:
'@vercel/edge-config':
specifier: ^0.4.1
version: 0.4.1
+ '@vercel/functions':
+ specifier: ^1.0.0
+ version: 1.0.0
'@vercel/og':
specifier: ^0.6.2
version: 0.6.2
@@ -7598,6 +7601,11 @@ packages:
resolution: {integrity: sha512-NdJxoaUI/AMvSu9dBM5MA1mxzk+Xo1sMrjyNRtNGsH5iKYyMdMWNUEvHdkyn1kN6opM03rQHYOYgXhBwNyIfdQ==}
dev: false
+ /@vercel/[email protected]:
+ resolution: {integrity: sha512-eF4G5IhTNYg4BbF5RgCdpwQfd3EakHh50tE8Rvs8/wgE2Ec7RIE5agj2fHbPMMjfkNkJQ95kpof+HVO0KDYPLg==}
+ engines: {node: '>= 16'}
+ dev: false
+
resolution: {integrity: sha512-OTe0KE37F5Y2eTys6eMnfopC+P4qr2ooXUTFyFPTplYSPwowmFk/HLD1FXtbKLjqsIH0SgekcJWad+C5uX4nkg==}
engines: {node: '>=16'}