การรวมแบ็กเอนด์แบบฟอร์ม Next.js (การทำงานของเราเตอร์แอปและเซิร์ฟเวอร์)
คู่มือการใช้งาน TypeScript และ React ฉบับสมบูรณ์สำหรับการจัดการแบบฟอร์มการติดต่อใน Next.js 14/15 โดยใช้จุดสิ้นสุด Lugatu และการดำเนินการของเซิร์ฟเวอร์โดยไม่มีการเปิดเผยข้อมูลประจำตัว
1. สถาปัตยกรรมเราเตอร์แอป Next.js พร้อมการดำเนินการของเซิร์ฟเวอร์
การดำเนินการของเซิร์ฟเวอร์ Next.js ดำเนินการบนเซิร์ฟเวอร์โดยเฉพาะ ทำให้เหมาะอย่างยิ่งสำหรับการส่งข้อซักถามด้วยคีย์ Lugatu API ส่วนตัวโดยไม่ต้องเปิดเผยความลับในชุดไคลเอนต์
'use server';
export async function submitContact(prevState: any, formData: FormData) {
// Silent rejection of automated bot submissions via honeypot trap
const honeypot = (formData.get('_gotcha') as string) || '';
if (honeypot) {
return { success: true, error: null };
}
const name = formData.get('name') as string;
const email = formData.get('email') as string;
const message = formData.get('message') as string;
if (!name || !email || !message) {
return { success: false, error: 'All fields are required.' };
}
const payload = {
fields: { name, email, message },
};
try {
const res = await fetch(`https://www.lugatu.com/api/v1/forms/${process.env.LUGATU_FORM_ID}/submissions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.LUGATU_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': crypto.randomUUID(),
},
body: JSON.stringify(payload),
});
if (!res.ok) {
return { success: false, error: 'Failed to submit form.' };
}
return { success: true, error: null };
} catch (err) {
return { success: false, error: 'Network error occurred.' };
}
}
2. ส่วนประกอบแบบฟอร์มฝั่งไคลเอ็นต์พร้อมสถานะการโหลด
เชื่อมต่อ Server Action ของคุณกับส่วนประกอบไคลเอนต์ React 19 / Next.js พร้อมตัวบ่งชี้การโหลดที่สามารถเข้าถึงได้
'use client';
import { useActionState } from 'react';
import { submitContact } from '@/app/actions/contact';
export function ContactForm() {
const [state, formAction, isPending] = useActionState(submitContact, { success: false, error: null });
return (
<form action={formAction} className="space-y-4 max-w-md">
<div>
<label className="block text-sm font-semibold mb-1">Name</label>
<input name="name" required className="w-full border rounded-xl p-2.5" />
</div>
<div>
<label className="block text-sm font-semibold mb-1">Email</label>
<input type="email" name="email" required className="w-full border rounded-xl p-2.5" />
</div>
<div>
<label className="block text-sm font-semibold mb-1">Message</label>
<textarea name="message" rows={4} required className="w-full border rounded-xl p-2.5" />
</div>
<input type="text" name="_gotcha" style={{ display: 'none' }} tabIndex={-1} autoComplete="off" />
<button type="submit" disabled={isPending} className="btn-primary-editorial w-full py-2.5">
{isPending ? 'Sending...' : 'Send Message'}
</button>
{state.success && <p className="text-emerald-700 text-sm">Thank you! Your message was received.</p>}
{state.error && <p className="text-red-700 text-sm">{state.error}</p>}
</form>
);
}
ความปลอดภัยและการรับรองความถูกต้องไม่แปรผัน
คำแนะนำด้านความปลอดภัย: ห้ามขึ้นต้น LUGATU_API_KEY ด้วย NEXT_PUBLIC_ เฉพาะตัวแปรฝั่งเซิร์ฟเวอร์ส่วนตัวเท่านั้นที่สามารถเก็บคีย์ API ที่ใช้งานอยู่ของคุณได้อย่างปลอดภัย
การแก้ไขปัญหาและข้อผิดพลาดในการนำเข้าทั่วไป
คุณกำลังทำการเรียก fetch() จากโค้ดเบราว์เซอร์ฝั่งไคลเอ็นต์ด้วยคีย์ API เปลี่ยนไปใช้การดำเนินการเซิร์ฟเวอร์ Next.js หรือใช้จุดสิ้นสุดของแบบฟอร์มสาธารณะโดยไม่มีคีย์ API
คำถามที่พบบ่อย
ฉันสามารถปรับใช้สิ่งนี้กับเพจ Vercel หรือ Cloudflare ได้หรือไม่
ใช่. การดำเนินการของเซิร์ฟเวอร์ทำงานได้อย่างราบรื่นบนแพลตฟอร์มแบบไร้เซิร์ฟเวอร์และรันไทม์ Edge ที่ Next.js รองรับ
พร้อมที่จะสร้างจุดสิ้นสุดของแบบฟอร์มแล้วหรือยัง?
เริ่มต้นฟรีในไม่กี่วินาที ไม่ต้องใช้บัตรเครดิต