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).
This commit is contained in:
+13
-20
@@ -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 }
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user