Sub topic 13
Bundler and framework support, and refreshing data after a mutation
This chapter assumes you can write Server Functions and use them as form actions, and that you know the security rules from the earlier chapters. It covers two practical questions. What do you need installed for "use server" to work? How does the page show new data after a Server Function changes it?
Why a bundler or framework is required
"use server" is not something the browser or plain React understands on its own. A bundler has to process it at build time. For each Server Function, the bundler does three things:
- It keeps the function body in the server bundle.
- It generates a reference for the client bundle. The reference is a small stub that holds an identifier, not your code.
- It makes the server expose an endpoint that accepts calls to that identifier.
When a Client Component calls createNote, it is calling that stub. The stub sends an HTTP request, and the server runs the real function.
Because this needs bundler and server cooperation, you normally use a framework or a bundler plugin that supports React Server Components. Examples are Next.js (App Router), React Router in framework mode, Waku, Parcel, and Vite with an RSC plugin. Support levels and setup steps differ between these tools and change between releases, so check your tool's documentation. Do not assume that a plain client-only Vite or Create React App project can run Server Functions.
Refreshing data after a mutation
A Server Function that inserts a row does not update what the user sees. The page still shows the old list until something re-renders it with fresh data.
Frameworks solve this by letting the Server Function say which data is now stale. The framework then sends the updated UI in the same response as the function's result, so the client needs no second request.
In Next.js, the helpers live in next/cache:
// app/actions.js
'use server';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { db } from './db';
export async function createNote(prevState, formData) {
const title = String(formData.get('title') ?? '').trim();
if (title.length === 0) {
return { error: 'Title is required.' };
}
await db.notes.insert({ title });
revalidatePath('/notes');
redirect('/notes');
}Here db is a placeholder for your data layer. revalidatePath('/notes') marks the cached output for that route as stale. redirect then sends the user to the refreshed page.
Note two details. Call redirect last, because it works by throwing a special error that stops the function. Do not wrap it in a try/catch that swallows the error. Also, return validation errors before you mutate or redirect, as the example does.
If several pages show the same data, path-based refreshing gets tedious. Next.js also offers tags. You tag a fetch or cached query with a name, then call revalidateTag('notes') from the Server Function to refresh everything with that tag. The exact tagging API has changed between Next.js versions, so confirm it for your version.
The round trip
Common errors
| Symptom | Likely cause |
|---|---|
| The list does not change after a successful insert | The function never marked the data stale, so the route kept serving cached output. |
| `redirect` seems to do nothing, or the error shows in logs | It was called inside `try`/`catch` and the thrown error was caught. |
| Build error mentioning `"use server"` in a client-only project | The bundler has no RSC support configured. |
| An old value appears after navigating back | Client-side router cache. Check how your framework caches visited routes. |
A second source of confusion is that revalidatePath and revalidateTag are framework APIs, not part of React. Other frameworks use different names, such as route reloads or cache invalidation calls. The pattern is the same: mutate, mark stale, let the framework re-render.
Exercise
A Server Function inserts a note and returns { error: null }, but the notes list on the page stays the same until the user reloads. The project uses Next.js. What is missing, and where should the fix go?
Show answer
The function never marks the cached data as stale. Inside the Server Function, after the insert succeeds, call revalidatePath for the route that shows the list (or revalidateTag if the data is tagged). Then the framework returns the refreshed UI along with the function's result.
Exercise
Why should redirect() be the last call in a Server Function, and why must it not be inside a try/catch that ignores errors?
Show answer
redirect works by throwing a special error that the framework catches to perform the navigation. Code after it will not run, and a catch block that swallows the error cancels the redirect.