Skip to content

Server rendering and state ​

Where each file runs ​

FileRuns onWhy
page.jsserverIt calls the API before sending the page
ProductList.jsserverIt only displays. The Delete button is a plain HTML form
ProductForm.jsserver and browserIt shows "Ajout…" and errors: it needs React state
actions.jsserverThese are Server Actions
lib/products.jsserverIt 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.