From c7bc4c724f0ffb5aff58537fde303daccb8291bb Mon Sep 17 00:00:00 2001 From: Ajay Ghanwat Date: Mon, 13 Apr 2026 12:50:20 +0530 Subject: [PATCH] fix: make contact form response instant by sending email in background The POST handler previously awaited sendContactEmail() which blocked the HTTP response until SMTP delivery completed. Now the email is fired off as a background promise with error logging/Sentry capture in .catch(), so users get an immediate 200 response (~130ms vs seconds). --- src/pages/api/contact.ts | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/pages/api/contact.ts b/src/pages/api/contact.ts index dd1381a..8fdf1bf 100644 --- a/src/pages/api/contact.ts +++ b/src/pages/api/contact.ts @@ -302,26 +302,19 @@ export const POST: APIRoute = async ({ request, clientAddress }) => { ); } - // Send email - try { - await sendContactEmail(formData); - } catch { - logApiRequest({ - scope: 'api.contact', - method: 'POST', - path: '/api/contact', - status: 500, - ip, - durationMs: Date.now() - startTime, + // Fire-and-forget: send email in background so the user gets an instant response. + // Errors are logged/reported but never block the HTTP reply. + sendContactEmail(formData).catch(async (err) => { + logger.error('api.contact', 'Background email delivery failed', { + email: formData.email, + subject: formData.subject, + error: err instanceof Error ? err.message : String(err), }); - return new Response( - JSON.stringify({ - success: false, - error: 'Failed to send message. Please try again or email us directly.', - }), - { status: 500, headers: rateLimitHeaders } - ); - } + await captureException(err, { + scope: 'api.contact.background', + extra: { email: formData.email, subject: formData.subject }, + }); + }); logApiRequest({ scope: 'api.contact', @@ -334,7 +327,7 @@ export const POST: APIRoute = async ({ request, clientAddress }) => { return new Response( JSON.stringify({ success: true, - message: "Message sent successfully! We'll get back to you within 24 hours.", + message: "Message received! We'll get back to you within 24 hours.", }), { status: 200, headers: rateLimitHeaders } );