Server Functions and forms
What you need to know
A Server Function is an async function that can be invoked from the client through React’s framework integration. When it handles a form submission or other action, it is commonly called a Server Action. The use server directive marks the callable server boundary, not every Server Component.
By the end, you can: identify use server boundary; parse FormData safely.
Read a form field safely
export function readTitle(data: FormData): string | null { const values = data.getAll('title'); if (values.length !== 1 || typeof values[0] !== 'string') return null; const title = values[0].trim(); return title.length >= 1 && title.length <= 60 ? title : null;}Expected behavior (not framework-tested): One nonblank title of at most 60 JavaScript UTF-16 code units is accepted; a duplicate, File or whitespace-only string is rejected.
How it works
Place 'use server' at the start of an async function for an inline action, or at module scope for exported async functions intended for server invocation. A Server Component may render a form whose action is that function. The form sends named fields using FormData. A field can be absent, a string or a File; get selects the first entry and getAll allows a duplicate-field policy. Validate data inside the function even if the input has required or maxlength attributes. TypeScript does not make the serialized request trustworthy.
For an expected validation failure, return a documented serializable result when the consuming UI handles that state (next chapter covers useActionState). In a simple successful form, redirect to a result page after the operation. React’s form action integration is different from a string URL or the usual onSubmit event. Do not promise identical progressive enhancement in every client configuration without checking it. The V6 fixture uses an intentionally public, stateless validation demo only; no persistent private mutation is enabled.
Connect an inline action to a form
// app/title-check/page.tsx — public, no writesimport { redirect } from 'next/navigation';import { readTitle } from '../../lib/forms';export default function Page() { async function check(data: FormData) { 'use server'; if (readTitle(data) === null) redirect('/title-check/invalid'); redirect('/title-check/valid'); } return <form action={check}> <label htmlFor="title">Book title</label> <input id="title" name="title" required maxLength={60} /> <button type="submit">Check title</button> </form>;}Expected behavior (not framework-tested): Submitting a valid title navigates to a fixed success route; an invalid submission navigates to a fixed error route. It does not store the title.
Common mistake
Do not assume String(data.get("title")) validates a field: null or a File can be converted into a misleading string. Use explicit type checks. Do not put an arbitrary untrusted URL into a redirect, and do not use use server merely to indicate a component runs on the server.
Practice and verification
Trace a valid title, a missing field, repeated title, a File and spaces-only input. Compare native form behavior with the server validation. Explain why the server must check even if browser validation is enabled. The next chapter will add inline error and pending state.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Forms · Mutating · Data Security · Redirect