0
0
NextJSframework~10 mins

On-demand revalidation in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - On-demand revalidation
User requests page
Serve cached page
Trigger revalidation request
Next.js regenerates page in background
Update cache with new page
Next user gets updated page
When a user requests a page, Next.js serves the cached version immediately. Then, it triggers a background regeneration to update the cache for future users.
Execution Sample
NextJS
export default async function handler(req, res) {
  await res.revalidate('/path')
  res.send('Revalidated')
}
This code triggers on-demand revalidation for the '/path' page when the GET request is made.
Execution Table
StepActionResultCache StateUser Response
1User requests '/path'Serve cached pageCache has old pageOld page content shown
2Revalidation triggeredStart background regenerationCache still old pageNo change to user
3Page regeneratedNew page generatedCache updated with new pageNo immediate user impact
4Next user requests '/path'Serve updated cached pageCache has new pageNew page content shown
5No further revalidationCache remains freshCache has new pageNew page content shown
💡 Revalidation updates cache in background; users always get cached page immediately.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
cacheold pageold pagenew pagenew pagenew page
user_responsenoneold page shownold page shownnew page shownnew page shown
Key Moments - 2 Insights
Why does the first user still see the old page after revalidation is triggered?
Because revalidation happens in the background after serving the cached page, the first user gets the old cached content immediately (see execution_table step 1 and 2).
When does the cache update with the new page content?
The cache updates only after the background regeneration finishes (see execution_table step 3), so new users get the updated page.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache state after step 2?
ACache is empty
BCache has new page
CCache has old page
DCache is invalid
💡 Hint
Check the 'Cache State' column for step 2 in the execution_table.
At which step does the user start seeing the updated page content?
AStep 1
BStep 4
CStep 2
DStep 3
💡 Hint
Look at the 'User Response' column in the execution_table to find when new content is served.
If revalidation took longer, how would the cache state change at step 3?
ACache remains old page longer
BCache updates immediately
CCache clears automatically
DCache serves error page
💡 Hint
Consider the cache update timing shown in variable_tracker and execution_table step 3.
Concept Snapshot
On-demand revalidation lets Next.js update cached pages after serving them.
Users get fast responses from cache.
Revalidation runs in background to refresh cache.
New users see updated pages after regeneration.
Use res.revalidate(path) in API routes to trigger.
Full Transcript
On-demand revalidation in Next.js means when a user requests a page, Next.js serves the cached version immediately for speed. Then, it triggers a background process to regenerate the page and update the cache. This way, the first user sees the old cached page, but the cache updates for future users. The process involves serving the cached page, triggering revalidation, regenerating the page, updating the cache, and then serving the updated page to new users. This keeps pages fresh without slowing down user experience. Developers trigger this using res.revalidate('/path') in API routes or server actions.