Two forms with Server Actions are sent before the page's JavaScript loads. What does the server answer, and what does the next visitor see?
The tea shop's newsletter page has a form whose Server Action saves the email and calls redirect. The Mint tea page lists its reviews and has a form whose Server Action saves a review and does nothing else. Both pages are Server Components. A visitor on a slow phone sends each form before the page's JavaScript has loaded, and later someone else opens the Mint tea page. What does the server answer each time?
app/layout.tsxexport const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
lib/shop.ts// A stand-in for the shop's database: everything lives in the server's memory.
const reviews: string[] = [];
const subscribers: string[] = [];
export async function getReviews() {
return reviews;
}
export async function saveReview(text: string) {
reviews.push(text);
}
export async function addSubscriber(email: string) {
subscribers.push(email);
}
app/actions.ts"use server";
import { redirect } from "next/navigation";
import { addSubscriber, saveReview } from "@/lib/shop";
export async function subscribe(formData: FormData) {
await addSubscriber(String(formData.get("email") ?? ""));
redirect("/newsletter/thanks");
}
export async function postReview(formData: FormData) {
const text = String(formData.get("review") ?? "").trim();
if (text) await saveReview(text);
}
app/newsletter/page.tsximport { subscribe } from "@/app/actions";
export default function NewsletterPage() {
return (
<main>
<h1>The Leaf and Kettle letter</h1>
<form action={subscribe}>
<input type="email" name="email" aria-label="Email" />
<button>Subscribe</button>
</form>
</main>
);
}
app/newsletter/thanks/page.tsxexport default function ThanksPage() {
return <h1>Thanks for subscribing</h1>;
}
app/teas/mint/page.tsximport { getReviews } from "@/lib/shop";
import { postReview } from "@/app/actions";
export default async function MintTeaPage() {
const reviews = await getReviews();
return (
<main>
<h1>Mint tea</h1>
{reviews.length ? (
<ul>
{reviews.map((review) => (
<li key={review}>{review}</li>
))}
</ul>
) : (
<p>No reviews yet.</p>
)}
<form action={postReview}>
<textarea name="review" aria-label="Review" />
<button>Post review</button>
</form>
</main>
);
}
It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build and run with next start. What happens:
- You run
next buildandnext start. - Before
/newsletterhas loaded its JavaScript, a visitor typesasha@example.comand presses Subscribe. - Before
/teas/minthas loaded its JavaScript, the visitor typesLovely with honey.and presses Post review. - Someone else opens
/teas/mint.
The answer
Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.
Checking your sign-in…