Discover how to keep your site lightning-fast and always fresh without annoying reloads!
Why Revalidation patterns in NextJS? - Purpose & Use Cases
Imagine you have a website showing live sports scores. You try to update the scores manually by refreshing the whole page or asking users to reload to see new results.
Manually refreshing the entire page wastes time and bandwidth. Users see outdated info until they reload, causing frustration and confusion. It's hard to keep data fresh without slowing down the site.
Revalidation patterns in Next.js let your site update only the parts that changed automatically. The page stays fast and shows fresh data by re-fetching content in the background when needed.
export async function getStaticProps() {
const data = await fetchData()
return { props: { data } }
}
// Data stays static until next buildexport async function getStaticProps() {
const data = await fetchData()
return { props: { data }, revalidate: 10 }
}
// Data refreshes every 10 seconds automaticallyYour website can serve fast static pages while keeping content fresh and up-to-date without manual reloads or slow full refreshes.
News websites use revalidation to show breaking stories quickly. Readers get instant updates without waiting for a full site rebuild or manual refresh.
Manual page refreshes cause slow, outdated content.
Revalidation patterns update data automatically in the background.
This keeps pages fast and content fresh for users.
Practice
revalidate export in Next.js control?Solution
Step 1: Understand the role of
Therevalidaterevalidateexport sets the time in seconds for Next.js to update a cached page after it was first generated.Step 2: Compare options with this behavior
Only How often a cached page updates after initial generation correctly describes this update frequency control. Other options describe unrelated concepts.Final Answer:
How often a cached page updates after initial generation -> Option AQuick Check:
Revalidate controls update frequency = D [OK]
- Confusing revalidate with cache size
- Thinking revalidate delays first page generation
- Mixing revalidate with rendering priority
Solution
Step 1: Recall correct export syntax in Next.js
To export a constant, the syntax isexport const name = value;.Step 2: Match this with revalidate usage
export const revalidate = 60; matches this pattern exactly. Other options have syntax errors or misplaced keywords.Final Answer:
export const revalidate = 60; -> Option BQuick Check:
Correct export syntax = A [OK]
- Omitting 'const' keyword
- Using parentheses incorrectly
- Placing export keyword after variable
export const revalidate = 10;
export default function Page() {
return <div>Time: {Date.now()}</div>;
}What will happen if you visit this page multiple times within 10 seconds?
Solution
Step 1: Understand revalidate timing effect
Withrevalidate = 10, Next.js caches the page and only regenerates it after 10 seconds.Step 2: Predict page behavior on multiple visits
Within 10 seconds, the cached page is served, so the timestamp stays the same. After 10 seconds, it updates.Final Answer:
The page shows the same timestamp for 10 seconds before updating -> Option DQuick Check:
Revalidate caches page for 10 seconds = B [OK]
- Assuming Date.now() updates on every visit
- Thinking revalidate disables page updates
- Confusing revalidate with client-side rendering
export const revalidate = '30';
export default function Page() {
return <div>Hello</div>;
}What is the problem with this code?
Solution
Step 1: Check the type of revalidate value
Therevalidateexport expects a number representing seconds, not a string.Step 2: Identify the error caused by wrong type
Using a string will cause Next.js to ignore or error on revalidation timing.Final Answer:
revalidate must be a number, not a string -> Option AQuick Check:
revalidate requires number type = A [OK]
- Using quotes around the number
- Thinking revalidate needs async function
- Misunderstanding export rules
export const revalidate = ?;
Choose the best value and explain why.
Solution
Step 1: Convert 5 minutes to seconds
5 minutes equals 300 seconds, so revalidate should be set to 300.Step 2: Understand caching vs freshness tradeoff
Setting revalidate to 300 caches the page for 5 minutes, improving performance but still updating regularly.Step 3: Evaluate other options
0 disables caching (no performance gain), 60 updates too often, Infinity never updates (stale data).Final Answer:
300, because 300 seconds equals 5 minutes, balancing freshness and caching -> Option CQuick Check:
5 minutes = 300 seconds revalidate [OK]
- Using 0 thinking it improves performance
- Choosing too short or too long revalidate values
- Confusing minutes with seconds
Ready to prove it?
Pass the test and this pattern moves unknown
