Server rendering and state
Where each file runs
| File | Runs on | Why |
|---|---|---|
page.js | server | It calls the API before sending the page |
ProductList.js | server | It only displays. The Delete button is a plain HTML form |
ProductForm.js | server and browser | It shows "Ajout…" and errors: it needs React state |
actions.js | server | These are Server Actions |
lib/products.js | server | It reads API_URL, which does not exist in the browser |
No shared state
The front end keeps no data between two requests: no store, no cache. The source of truth is the API database.
After an add or a delete, the action calls revalidatePath('/'). Next.js then rebuilds the / page, which calls the API again: the displayed list is always the one in the database.
The only browser-side state is the form's, handled by useActionState: { error } and pending (sending in progress).
Why force-dynamic
js
export const dynamic = 'force-dynamic';Without this line, Next.js would try to pre-render the page during next build. It would call the API at that point, while it does not exist in the pipeline's build job: the build would fail. With force-dynamic, the page is always built at request time.
The details are in The product list.