0
0
NextJSframework~10 mins

Revalidation strategies (time-based) in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Revalidation strategies (time-based)
Initial Request
Serve Cached Page
Check Revalidation Time
Fetch New Data
Update Cache
Next Request Uses Updated Cache
This flow shows how Next.js serves a cached page and triggers background revalidation after a set time, updating the cache for future requests.
Execution Sample
NextJS
export async function getStaticProps() {
  const data = await fetchData();
  return {
    props: { data },
    revalidate: 10
  };
}
This code fetches data at build time and sets the page to revalidate every 10 seconds.
Execution Table
StepRequest Time (s)Cache StatusRevalidation Check (time since last update)Action TakenPage Served
10No cacheN/AFetch data and cacheFresh page with data
25Cached (5s old)5 < 10Serve cached pageCached page
310Cached (10s old)10 >= 10Serve cached page + trigger background revalidationCached page
411RevalidatingN/AFetch new data and update cacheCached page (still)
515Cached (4s old)4 < 10Serve cached pageUpdated cached page
621Cached (10s old)10 >= 10Serve cached page + trigger background revalidationUpdated cached page
722RevalidatingN/AFetch new data and update cacheCached page (still)
830Cached (8s old)8 < 10Serve cached pageUpdated cached page
ExitN/AN/AN/AStop after last requestN/A
💡 Execution stops after the last request at 30 seconds.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
Cache Timestamp (seconds)N/A0001111112222
Cache Age (seconds)N/A0510041008
Revalidation TriggeredNoNoNoYesNoNoYesNoNo
Key Moments - 3 Insights
Why does the page serve the cached version even when revalidation is triggered?
Because Next.js serves the existing cached page immediately to avoid delay, while revalidation happens in the background (see execution_table step 3).
What happens if a request comes before the revalidation finishes?
The cached page is served again until the background revalidation updates the cache (see execution_table step 4 and 5).
How is the cache age calculated for revalidation?
Cache age is the time difference between the current request and the last cache update timestamp (see variable_tracker Cache Age).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache age at step 5?
A4 seconds
B10 seconds
C5 seconds
D0 seconds
💡 Hint
Check the 'Cache Age (seconds)' column at step 5 in the variable_tracker.
At which step does Next.js trigger background revalidation for the first time?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Revalidation Check' and 'Action Taken' columns in the execution_table.
If the revalidate time was changed to 5 seconds, what would happen at step 2?
AServe cached page only
BFetch new data immediately
CServe cached page and trigger revalidation
DServe stale page without revalidation
💡 Hint
Compare the 'Revalidation Check' logic with the new revalidate time in the concept_flow.
Concept Snapshot
Next.js time-based revalidation:
- Use 'revalidate' in getStaticProps to set seconds.
- Serve cached page immediately.
- After 'revalidate' seconds, trigger background fetch.
- Update cache after fetch.
- Next requests get updated page.
- Improves performance and freshness.
Full Transcript
This visual execution shows how Next.js handles time-based revalidation. Initially, the page is built and cached. For requests before the revalidate time, the cached page is served directly. When the revalidate time passes, Next.js serves the cached page but triggers a background fetch to update the cache. Subsequent requests get the updated page. Variables like cache timestamp and age track when to revalidate. This strategy balances fast responses with fresh data.