What if your website could update itself instantly every time you make a change?
Why Revalidation after mutation in NextJS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you update some data on your website, like changing a blog post or adding a comment, but the page still shows the old information because it doesn't refresh automatically.
Manually refreshing or rebuilding pages after every change is slow, confusing, and can cause users to see outdated content. It's easy to forget to update, leading to mistakes and a bad experience.
Revalidation after mutation lets Next.js automatically refresh the data on your pages right after you change it, so users always see the latest content without waiting or manual steps.
await updateData(); // then manually trigger page refresh or rebuildawait updateData();
await revalidatePath('/page-path'); // Next.js updates page data automaticallyThis makes your app feel fast and reliable by instantly showing fresh data after any update.
When you post a new comment on a blog, the comment appears immediately without needing to reload the whole page.
Manual page updates are slow and error-prone.
Revalidation after mutation automates refreshing data after changes.
Users always see the newest content instantly.
Practice
Solution
Step 1: Understand mutation effect
When data changes (mutation), the UI might show old data if not updated.Step 2: Purpose of revalidation
Revalidation reloads or refreshes data so the UI reflects the latest changes.Final Answer:
To update the page data after a change so the UI shows fresh information -> Option AQuick Check:
Revalidation updates data = A [OK]
- Thinking revalidation stops data saving
- Confusing revalidation with full page reload
- Believing it disables user actions
Solution
Step 1: Identify method for revalidation
Next.js App Router providesrouter.refresh()to reload data without full page reload.Step 2: Differentiate from other methods
router.reload()reloads whole page,router.push()androuter.replace()change routes.Final Answer:
router.refresh() -> Option DQuick Check:
Revalidation uses router.refresh() = A [OK]
- Using router.reload() which reloads full page
- Confusing route navigation methods with revalidation
- Trying to use router.replace() for data refresh
import { useRouter } from 'next/navigation';
export default function UpdateButton() {
const router = useRouter();
async function handleClick() {
await fetch('/api/update', { method: 'POST' });
router.refresh();
}
return Update;
}What happens when the button is clicked?
Solution
Step 1: Analyze handleClick function
It calls the API with POST, then callsrouter.refresh()to reload data.Step 2: Understand router.refresh() effect
This refreshes the current route's data without a full page reload or navigation.Final Answer:
The API is called, then the page data refreshes without full reload -> Option AQuick Check:
API call + router.refresh() updates data = C [OK]
- Thinking router.refresh() reloads entire page
- Assuming router.refresh() navigates to API route
- Believing nothing happens due to syntax error
import { useRouter } from 'next/navigation';
export default function DeleteButton() {
const router = useRouter;
async function handleDelete() {
await fetch('/api/delete', { method: 'DELETE' });
router.refresh();
}
return Delete;
}Solution
Step 1: Check router initialization
Code usesconst router = useRouter;missing parentheses, so router is a function reference, not the router object.Step 2: Effect on router.refresh()
Callingrouter.refresh()fails because router is not an object but a function reference.Final Answer:
router is not called as a function, so router.refresh() fails -> Option CQuick Check:
useRouter missing () causes error = D [OK]
- Forgetting parentheses on useRouter()
- Thinking DELETE method is invalid
- Confusing onClick with onSubmit
Solution
Step 1: Understand goal
After mutation, the UI must show updated list without full reload or navigation.Step 2: Evaluate options
router.push('/current-page')navigates and reloads route,window.location.reload()reloads full page, and doing nothing won't update data.Step 3: Correct method
router.refresh()triggers Next.js to revalidate data and update UI efficiently.Final Answer:
Call router.refresh() after mutation to revalidate and update data -> Option BQuick Check:
Use router.refresh() to update data after mutation = B [OK]
- Using router.push() causing unnecessary navigation
- Reloading full page losing state
- Assuming UI updates automatically without refresh
