One button's Server Action calls revalidatePath and the other's doesn't. What does the page show after each click?
The tea shop's tasting journal lists the notes saved so far, read from the database by the page, a Server Component. Its form has one field and two buttons, each with its own Server Action in formAction: Save only saves the note, and Save and revalidate saves it and then calls revalidatePath("/journal"). You save one note with each button, then load the page again. What does the page show after each step?
app/layout.tsxexport const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
lib/journal.ts// A stand-in for the shop's database: the notes live in the server's memory.
const notes: string[] = [];
export async function getNotes() {
return notes;
}
export async function saveNote(text: string) {
notes.push(text);
}
app/journal/actions.ts"use server";
import { revalidatePath } from "next/cache";
import { saveNote } from "@/lib/journal";
export async function save(formData: FormData) {
const note = String(formData.get("note") ?? "").trim();
if (note) await saveNote(note);
}
export async function saveAndRevalidate(formData: FormData) {
const note = String(formData.get("note") ?? "").trim();
if (note) await saveNote(note);
revalidatePath("/journal");
}
app/journal/page.tsximport { getNotes } from "@/lib/journal";
import { save, saveAndRevalidate } from "./actions";
export default async function JournalPage() {
const notes = await getNotes();
return (
<main>
<h1>Tasting journal</h1>
{notes.length ? (
<ul>
{notes.map((note) => (
<li key={note}>{note}</li>
))}
</ul>
) : (
<p>No notes yet.</p>
)}
<form>
<input name="note" aria-label="Note" />
<button formAction={save}>Save</button>
<button formAction={saveAndRevalidate}>Save and revalidate</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, and open/journalin a browser. - You type
Brewed oolonginto the Note field. - You click Save.
- You type
Tried mintinto the Note field. - You click Save and revalidate.
- You load
/journalagain.
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…