A tour, day by day
Open the files in this order, and find each idea before reading its explanation.
| Day | Idea | Where |
|---|---|---|
| 1 | Pages from folders; one root layout; a section's own not-found page | app/, app/layout.jsx, app/notes/[id]/not-found.jsx |
| 2 | Pages are server components; only the navigation and the form are client components | app/NavLinks.jsx, app/new/NoteForm.jsx |
| 3 | A [id] page reads params; the tag filter lives in searchParams; the current link is marked with usePathname | app/notes/[id]/page.jsx, app/page.jsx, app/NavLinks.jsx |
| 4 | The slow stats stream in their own <Suspense>; opening a note shows loading.jsx; error.jsx catches the rest | app/page.jsx, app/NoteStats.jsx, app/notes/[id]/loading.jsx, app/error.jsx |
| 5 | Saving and deleting are server actions, with validation through useActionState, and redirect afterwards | app/actions.js, app/new/NoteForm.jsx |
| 6 | The theme cookie; a title template and generateMetadata; a JSON API at /api/notes | app/layout.jsx, app/notes/[id]/page.jsx, app/api/notes/route.js |
Three design choices to notice
The form sends back everything it was given. On an error, addNote
returns the values as well as the errors, and each field uses them as its
defaultValue, so a mistake doesn't discard the submitted draft. Error regions
are present with role="alert", and invalid fields are marked with
aria-invalid and connected to their messages with aria-describedby:
export async function addNote(previous, formData) {
const values = {
title: String(formData.get("title") ?? "").trim(),
body: String(formData.get("body") ?? "").trim(),
tag: String(formData.get("tag") ?? ""),
};
const errors = {};
if (values.title === "") errors.title = "Give the note a title.";
else if (values.title.length > 60) errors.title = "Keep the title under 60 characters.";
if (values.body === "") errors.body = "Write the note itself.";
if (!TAGS.includes(values.tag)) values.tag = TAGS[0];
if (Object.keys(errors).length > 0) return { errors, values };
const note = await createNote(values);
revalidatePath("/");
redirect(`/notes/${note.id}`);
}Every action ends where the reader expects. Saving goes to the new note;
deleting goes to the list. revalidatePath("/") comes first, so the list is
fresh when they get there:
export async function removeNote(id) {
await deleteNote(id);
revalidatePath("/");
redirect("/");
}A key on the stats boundary. Changing the tag is a navigation, and React
keeps the old page on screen during a navigation until everything in an
already-visible <Suspense> is ready again. Without help, a tag click would seem
to do nothing for over a second while the stats were counted. A new key for
each tag makes it a new boundary, which shows its fallback at once:
<Suspense key={tag ?? "all"} fallback={<p className="muted">Counting…</p>}>Follow a request
Without running anything, list in order what happens on the server when the reader presses Delete note on note 2, up to the moment they see the list.
Hint 1: the idea
Trace the form-to-action-to-data-to-navigation path. State which work runs in the browser and which runs on the server.
Hint 2: a first step
Start in notes/[id]/page.jsx at the form, then follow removeNote in
actions.js.
Show one solution
- The form calls
removeNote(2): the id was bound with.bind(null, note.id). deleteNote(2)removes it fromlib/notes.js.revalidatePath("/")marks the list as out of date.redirect("/")sends the reader to the list.- The list page runs again: it reads
searchParams, loads the notes, and streams the stats into their<Suspense>.
Completion is saved in this browser only. It does not measure mastery.