Skip to content

Commit 2d38d48

Browse files
author
Miriad
committed
fix: use after() instead of fire-and-forget in video webhook
On Vercel serverless, functions terminate after the response is sent. The fire-and-forget pattern (promise.catch() without await) caused the video pipeline to silently die after setting audio_gen status. next/server's after() keeps the function alive for background work, allowing the full pipeline (ElevenLabs TTS → GCS → Remotion → GCS) to complete after returning 200 to Sanity.
1 parent ea617ff commit 2d38d48

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

app/api/webhooks/sanity-content/route.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NextResponse } from 'next/server';
1+
import { NextResponse, after } from 'next/server';
22
import { isValidSignature, SIGNATURE_HEADER_NAME } from '@sanity/webhook';
33
import { processVideoProduction } from '@/lib/services/video-pipeline';
44

@@ -82,10 +82,16 @@ export async function POST(request: Request) {
8282
);
8383
}
8484

85-
// Fire and forget — trigger pipeline in background, return 200 immediately
85+
// Use after() to run the pipeline after the response is sent.
86+
// On Vercel, serverless functions terminate after the response — fire-and-forget
87+
// (promise.catch()) silently dies. after() keeps the function alive for background work.
8688
console.log(`[WEBHOOK] Triggering video production for document: ${body._id}`);
87-
processVideoProduction(body._id).catch((error) => {
88-
console.log(`[WEBHOOK] Background processing error for ${body._id}:`, error);
89+
after(async () => {
90+
try {
91+
await processVideoProduction(body._id);
92+
} catch (error) {
93+
console.error(`[WEBHOOK] Background processing error for ${body._id}:`, error);
94+
}
8995
});
9096

9197
return NextResponse.json({ triggered: true }, { status: 200 });

0 commit comments

Comments
 (0)