Part 12 / Topic 1 of 7
'use server' — two legal places for one directive
A Server Function is any async function with 'use server' marking it — in a mutation context, Next.js and the React ecosystem call it a Server Action. The installed mutating-data.md guide states the two places the directive can go:
"You can place the directive at the top of an asynchronous function to mark the function as a Server Function, or at the top of a separate file to mark all exports of that file."
This project uses the second form, deliberately split across two files by purpose, not bundled into one:
// harness/app/swiggy-menu/reviews/actions.ts — the review-submission action
"use server";
export async function submitReview(prevState: ReviewFormState, formData: FormData) { /* section 3 */ }
// harness/app/swiggy-menu/toggle-actions.ts — the optimistic stock-toggle action
"use server";
export async function toggleAvailability(id: string, available: boolean) { /* section 7 */ }
A file-level 'use server' turns every export of that file into a Server Function — which is exactly why review-state.ts (the plain ReviewFormState type and its initial value, imported by both the Server Action and the Client Component form) is kept in its own third file, with no directive at all. Putting a type-only, non-function export inside a 'use server' file would be a category error: the directive's job is marking server-callable functions, not just "things used on the server."
🔍 Going deeper — the inline form works too, and composes differently:
mutating-data.mdalso shows'use server'written directly inside a Server Component's own function body (async function createPost(formData) { 'use server'; ... }, nested insideexport default function Page()). Neither of this part's two actions uses that form — both are imported into a Client Component (ReviewForm.tsx,AvailabilityToggle.tsx) instead, which only works because the installed docs name directly: "It's not possible to define Server Functions in Client Components. However, you can invoke them in Client Components by importing them from a file that has the'use server'directive at the top of it." The inline form is for a Server Component rendering its own<form>directly; this part's forms live inside Client Components (souseActionState/useFormStatus/useOptimistic— all client-only hooks — can wrap them), which is exactly the case the file-level directive exists for.
Done here? The practice set for Part 12 — including the solutions — is on the part page.