0
0
NextJSframework~10 mins

Revalidation after mutation in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Revalidation after mutation
User triggers mutation
Mutation function runs
Data changes on server
Trigger revalidation
Next.js fetches fresh data
Page updates with new data
Done
This flow shows how after changing data (mutation), Next.js re-fetches fresh data to update the page.
Execution Sample
NextJS
async function updateData() {
  await mutate('/api/data', async () => {
    await fetch('/api/data', { method: 'POST' });
    return fetch('/api/data').then(res => res.json());
  }, { revalidate: true });
}
This code updates data on the server and then revalidates the cache to update the page.
Execution Table
StepActionServer Data StateCache StatePage State
1User triggers updateData()Original dataCached original dataPage shows original data
2POST request sent to /api/dataData updated on serverCached original dataPage shows original data
3Fetch fresh data after POSTData updated on serverCache updated with fresh dataPage shows fresh data
4Revalidation triggeredData updated on serverCache updated with fresh dataPage shows fresh data
5DoneData updated on serverCache freshPage shows updated data
💡 Revalidation completes, cache and page show updated data
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Server DataOriginal dataUpdated dataUpdated dataUpdated dataUpdated data
CacheOriginal dataOriginal dataFresh dataFresh dataFresh data
PageShows original dataShows original dataShows fresh dataShows fresh dataShows fresh data
Key Moments - 2 Insights
Why does the page still show old data right after the POST request?
Because the cache is not updated immediately after the POST; it updates after fetching fresh data (see Step 2 and 3 in execution_table).
What triggers the page to update with new data?
The revalidation process updates the cache with fresh data, which causes the page to re-render with updated content (see Step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cache state right after the POST request (Step 2)?
ACache has fresh updated data
BCache still has original data
CCache is empty
DCache has corrupted data
💡 Hint
Check the 'Cache State' column at Step 2 in execution_table
At which step does the page start showing the updated data?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Page State' column in execution_table
If revalidate option was false, what would happen to the page state after mutation?
APage would never update automatically
BPage would show an error
CPage would update immediately
DPage would reload the whole app
💡 Hint
Think about the role of revalidation in updating cache and page
Concept Snapshot
Revalidation after mutation in Next.js:
- Mutation changes data on server
- Cache still holds old data initially
- Revalidation fetches fresh data
- Cache updates with fresh data
- Page re-renders showing updated content
Use mutate() with { revalidate: true } to trigger this flow.
Full Transcript
In Next.js, when you change data on the server using a mutation, the page does not update immediately because the cached data is still the old version. After the mutation, Next.js triggers a revalidation process that fetches fresh data from the server and updates the cache. Once the cache updates, the page re-renders to show the new data. This process ensures the user sees the latest information without a full page reload. Using the mutate function with the revalidate option set to true controls this behavior.