Bird
Raised Fist0
NextJSframework~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does setting the revalidate property in getStaticProps do in Next.js?
easy
A. It tells Next.js to update the static page automatically after the specified seconds.
B. It disables static generation and forces server-side rendering.
C. It caches the page forever without any updates.
D. It triggers a client-side fetch to update the page content.

Solution

  1. Step 1: Understand revalidate role in getStaticProps

    The revalidate property sets a time interval in seconds for Next.js to regenerate the static page in the background.
  2. Step 2: Effect of setting revalidate

    After the specified time, Next.js updates the static page automatically without manual rebuilds or disabling static generation.
  3. Final Answer:

    It tells Next.js to update the static page automatically after the specified seconds. -> Option A
  4. Quick Check:

    Time-based revalidation = automatic page update [OK]
Hint: Remember: revalidate sets auto-update time in seconds [OK]
Common Mistakes:
  • Thinking revalidate disables static generation
  • Confusing revalidate with client-side fetching
  • Assuming revalidate caches forever
2. Which of the following is the correct way to set a 10-second revalidation in getStaticProps?
easy
A. export async function getStaticProps() { return { props: {}, revalidate: null } }
B. export async function getStaticProps() { return { props: {}, revalidate: '10' } }
C. export async function getStaticProps() { return { props: {}, revalidate: true } }
D. export async function getStaticProps() { return { props: {}, revalidate: 10 } }

Solution

  1. Step 1: Check the type of revalidate

    The revalidate value must be a number representing seconds.
  2. Step 2: Validate each option's syntax

    export async function getStaticProps() { return { props: {}, revalidate: 10 } } uses a number 10 correctly. export async function getStaticProps() { return { props: {}, revalidate: '10' } } uses a string '10' which is invalid. The other options use boolean and null, which are incorrect types.
  3. Final Answer:

    export async function getStaticProps() { return { props: {}, revalidate: 10 } } -> Option D
  4. Quick Check:

    revalidate must be a number [OK]
Hint: Use a number for revalidate seconds, not string or boolean [OK]
Common Mistakes:
  • Using string instead of number for revalidate
  • Setting revalidate to true or null
  • Forgetting to return revalidate inside the returned object
3. Given this code snippet in getStaticProps:
export async function getStaticProps() {
  return {
    props: { time: Date.now() },
    revalidate: 5
  }
}
What will happen if you visit the page multiple times within 3 seconds?
medium
A. The page will show the same time value for all visits within 3 seconds.
B. The page will update time on every visit regardless of time.
C. The page will throw an error because revalidate is too short.
D. The page will never update the time value.

Solution

  1. Step 1: Understand revalidate timing

    The revalidate: 5 means Next.js regenerates the page at most every 5 seconds.
  2. Step 2: Behavior within 3 seconds

    Visiting within 3 seconds means the cached page is served with the same time value because regeneration hasn't happened yet.
  3. Final Answer:

    The page will show the same time value for all visits within 3 seconds. -> Option A
  4. Quick Check:

    Revalidate interval controls update frequency [OK]
Hint: Page updates only after revalidate seconds pass [OK]
Common Mistakes:
  • Expecting page to update on every visit
  • Thinking revalidate causes errors if too small
  • Assuming page never updates after first build
4. You set revalidate: 0 in getStaticProps. What is the problem with this code?
medium
A. It causes the page to never render.
B. It caches the page forever without updates.
C. It disables static generation and causes a build error.
D. It causes the page to regenerate on every request, similar to server-side rendering.

Solution

  1. Step 1: Understand revalidate: 0 meaning

    Setting revalidate to 0 disables Incremental Static Regeneration (ISR). The page is generated at build time and cached forever without background regeneration.
  2. Step 2: Effect on page behavior

    This results in no automatic updates, which is the problem if revalidation was intended, behaving like static generation without ISR.
  3. Final Answer:

    It caches the page forever without updates. -> Option B
  4. Quick Check:

    revalidate 0 = no ISR, cache forever [OK]
Hint: revalidate: 0 caches forever, no updates [OK]
Common Mistakes:
  • Thinking revalidate: 0 regenerates on every request
  • Believing it causes a build error
  • Assuming it prevents the page from rendering
5. You want a page to update its static content every 60 seconds but only if the content has changed. Which Next.js feature combined with revalidate helps achieve this efficiently?
hard
A. Use getServerSideProps instead of getStaticProps.
B. Use revalidate: false to disable updates and manually rebuild.
C. Use revalidate: 60 with Incremental Static Regeneration (ISR) and conditional data fetching.
D. Set revalidate: 0 to regenerate on every request.

Solution

  1. Step 1: Understand ISR with revalidate

    Incremental Static Regeneration (ISR) allows pages to update after a set time without full rebuilds.
  2. Step 2: Combine with conditional data fetching

    Fetching data conditionally inside getStaticProps ensures updates only when content changes, saving resources.
  3. Final Answer:

    Use revalidate: 60 with Incremental Static Regeneration (ISR) and conditional data fetching. -> Option C
  4. Quick Check:

    ISR + revalidate = efficient timed updates [OK]
Hint: ISR with revalidate controls timed updates smartly [OK]
Common Mistakes:
  • Using getServerSideProps which disables static caching
  • Setting revalidate to false which is invalid
  • Using revalidate 0 causing regen every request