Skip to content

Commit

Permalink
Merge pull request #442 from mfts/feat/waituntil
Browse files Browse the repository at this point in the history
feat: add waituntil for simple notifications
  • Loading branch information
mfts authored May 27, 2024
2 parents 005c890 + 67f863b commit 24945da
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
57 changes: 28 additions & 29 deletions lib/api/notification-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@ import { getTriggerClient } from "@/trigger";

import { log } from "@/lib/utils";

// TODO: cleanup after removing trigger for this

export default async function sendNotification({ viewId }: { viewId: string }) {
const client = getTriggerClient();
// const client = getTriggerClient();

if (!client) {
/** If client does not exist, use fetch to send notifications */
return await fetch(
`${process.env.NEXTAUTH_URL}/api/jobs/send-notification`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.INTERNAL_API_KEY}`,
},
body: JSON.stringify({ viewId: viewId }),
},
)
.then(() => {})
.catch((error) => {
log({
message: `Failed to fetch notifications job in _/api/views_ route. \n\n Error: ${error} \n\n*Metadata*: \`{viewId: ${viewId}}\``,
type: "error",
mention: true,
});
// if (!client) {
/** If client does not exist, use fetch to send notifications */
return await fetch(`${process.env.NEXTAUTH_URL}/api/jobs/send-notification`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.INTERNAL_API_KEY}`,
},
body: JSON.stringify({ viewId: viewId }),
})
.then(() => {})
.catch((error) => {
log({
message: `Failed to fetch notifications job in _/api/views_ route. \n\n Error: ${error} \n\n*Metadata*: \`{viewId: ${viewId}}\``,
type: "error",
mention: true,
});
}
});
// }

/** If client exists, use trigger to send notifications */
return await client.sendEvent({
name: "link.viewed",
payload: {
viewId: viewId,
},
});
// /** If client exists, use trigger to send notifications */
// return await client.sendEvent({
// name: "link.viewed",
// payload: {
// viewId: viewId,
// },
// });
}

export async function sendViewerInvitation({
Expand Down
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const nextConfig = {
},
transpilePackages: ["@trigger.dev/react"],
skipTrailingSlashRedirect: true,
supportsResponseStreaming: true,
experimental: {
outputFileTracingIncludes: {
"/api/mupdf/*": ["./node_modules/mupdf/lib/*.wasm"],
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@upstash/redis": "^1.30.1",
"@vercel/analytics": "^1.2.2",
"@vercel/blob": "^0.22.3",
"@vercel/functions": "^1.0.2",
"ai": "^2.2.37",
"autoprefixer": "^10.4.19",
"base-x": "^4.0.0",
Expand Down
4 changes: 3 additions & 1 deletion pages/api/views.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";

import { waitUntil } from "@vercel/functions";

import sendNotification from "@/lib/api/notification-helper";
import { sendVerificationEmail } from "@/lib/emails/send-email-verification";
import { getFile } from "@/lib/files/get-file";
Expand Down Expand Up @@ -269,7 +271,7 @@ export default async function handle(

if (link.enableNotification) {
console.time("sendemail");
await sendNotification({ viewId: newView.id });
waitUntil(sendNotification({ viewId: newView.id }));
console.timeEnd("sendemail");
}

Expand Down

0 comments on commit 24945da

Please sign in to comment.