Performance: Form actions with server functions
This affects how quickly a form submission is processed and how fast the page updates after submission.
Jump into concepts and practice - no test required
'use server' import { redirect } from 'next/navigation'; export const action = async (formData) => { // server-side processing return redirect('/success'); }; export default function MyForm() { return <form action={action}>...</form>; }
function MyForm() { const handleSubmit = async (event) => { event.preventDefault(); const data = new FormData(event.target); await fetch('/api/submit', { method: 'POST', body: data }); // client-side state update }; return <form onSubmit={handleSubmit}>...</form>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side fetch on submit | Multiple (form + state updates) | Multiple reflows on state change | Medium paint cost | [X] Bad |
| Server function form action | Minimal (form submit only) | Single reflow after redirect | Low paint cost | [OK] Good |
form actions with server functions in Next.js App Router?formData as parameter.export async function action(formData) {
const name = formData.get('name');
return new Response(`Hello, ${name}!`);
}formData.get('name') to get the input named 'name'.export async function action(formData) {
const email = formData.email;
return new Response(`Email: ${email}`);
}formData.get('fieldName'), not dot notation.export async function action(formData) {
const feedback = formData.get('feedback');
// Save feedback to database (omitted)
return redirect('/thank-you');
}formData.get('feedback') to get the input value.redirect() to navigate after processing.