Mutation and redirect
What you need to know
A successful mutation changes authoritative data first, then invalidates dependent views and optionally redirects. Cache invalidation cannot make a rejected or failed database write successful.
By the end, you can: sequence commit and invalidation; handle redirect control flow.
Mutation sequence (partial design, not installed)
// Assumed reviewed helpers enforce session, validation and atomic ownership.'use server';import { revalidatePath, updateTag } from 'next/cache';import { redirect } from 'next/navigation';import { saveAuthorizedNote } from './reviewed-note-service';export async function saveNote(data: FormData) { const saved = await saveAuthorizedNote(data); // commits or throws if (!saved) redirect('/notes/edit-error'); updateTag('public-note-index'); // only a deliberately public derived index revalidatePath('/notes'); redirect('/notes/saved');}Expected behavior (not framework-tested): If the authorized write commits, dependent public index data is expired and the notes path is invalidated before navigation. The referenced service is not supplied; do not copy this as a complete private mutation.
How it works
The order is important: validate and authorize; execute and commit the write; invalidate affected paths or tagged data; then redirect or return action state. revalidatePath targets route-related cached content. revalidateTag(tag, 'max') marks tagged data stale for stale-while-revalidate behavior; updateTag(tag) is action-only and immediately expires the tag to support a fresh read after a write. Path invalidation alone does not necessarily refresh every other route using the same tagged data. Do not depend on temporary broad client refresh behavior as a permanent contract.
redirect is control flow that terminates the current path by throwing an internal signal; in a Server Action the documented HTTP redirect behavior is 303. Place it outside a catch that handles write errors, otherwise the catch can mistakenly swallow a successful redirect. The database commit and cache update are separate systems: if invalidation fails after commit, report accurately and plan recovery instead of blindly repeating the mutation. Do not claim exactly-once behavior from a form submission. The private mutation below is a design sketch, not an exposed route.
Choose invalidation based on freshness
// Partial authorized operation, called only after commit.import { revalidateTag, revalidatePath } from 'next/cache';export function afterCatalogChange() { revalidateTag('catalog', 'max'); revalidatePath('/catalog');}Expected behavior (not framework-tested): The catalog tag becomes stale and the catalog route is targeted. The next applicable visit may serve stale data while regeneration runs; this is not immediate global visibility.
Common mistake
Calling redirect inside a broad try/catch and translating every thrown value into “Save failed” can misreport success. Also avoid invalidating before the write commits or using the same tag for private user records. Choose a targeted policy after the authoritative result is known.
Practice and verification
Compare three cases: validation fails before write, write commits and invalidation succeeds, write commits but invalidation fails. State what the user should see and which retry/recovery steps are safe. Then choose updateTag or revalidateTag for a public title that should or need not be visible immediately to the editor.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Mutating · Redirect · Path · Tag · Update Tag