What if your site could update itself instantly, only where it needs to?
Why On-demand revalidation in NextJS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a website showing product prices that change often. Every time a price updates, you have to manually refresh the whole site or wait for a scheduled update.
Manually refreshing or waiting for fixed updates means visitors see old prices for a long time. This causes confusion and lost sales because the site is not up-to-date instantly.
On-demand revalidation lets your site update only the changed pages right when you want. It keeps your site fast and fresh without rebuilding everything all the time.
rebuild entire site every hour
trigger revalidation for specific page on data changeYou can instantly update parts of your site when data changes, giving users fresh content without slowing down your whole site.
An online store updates a product price. With on-demand revalidation, only that product page refreshes immediately, so customers always see the latest price.
Manual updates cause delays and stale content.
On-demand revalidation updates only what changed, instantly.
This keeps your site fast and always fresh for users.
Practice
Solution
Step 1: Understand static site generation
Static pages are pre-built and served fast but can become outdated.Step 2: Role of on-demand revalidation
It updates only specific pages instantly without rebuilding the whole site, keeping content fresh.Final Answer:
To update specific static pages instantly without rebuilding the entire site -> Option CQuick Check:
On-demand revalidation = update specific pages fast [OK]
- Thinking it rebuilds the entire site
- Confusing it with disabling static generation
- Assuming pages never update
Solution
Step 1: Identify the method for revalidation
Next.js providesres.revalidate(path)to trigger revalidation of a page.Step 2: Confirm method usage in API route
This method is called inside an API route handler to update the static page at the given path.Final Answer:
res.revalidate(path) -> Option DQuick Check:
API route uses res.revalidate() to update pages [OK]
- Using non-existent methods like res.refresh()
- Confusing with client-side reload methods
- Misspelling the method name
export default async function handler(req, res) {
if (req.method === 'POST' && req.query.secret === process.env.MY_SECRET) {
await res.revalidate('/blog/post-1')
return res.json({ revalidated: true })
}
return res.status(401).json({ message: 'Invalid token' })
}Solution
Step 1: Check request method and secret
The handler checks if the method is POST and the secret matches the environment variable.Step 2: Call res.revalidate on the specific page
If conditions pass, it callsres.revalidate('/blog/post-1')to update that page only.Final Answer:
The page '/blog/post-1' will be revalidated and a success JSON returned -> Option AQuick Check:
Correct secret + POST triggers revalidation [OK]
- Assuming entire site rebuilds
- Ignoring secret check
- Thinking client reload happens automatically
export default async function handler(req, res) {
if (req.method === 'GET' && req.query.secret === process.env.SECRET) {
await res.revalidate('/home')
res.json({ revalidated: true })
} else {
res.status(401).json({ message: 'Unauthorized' })
}
}Solution
Step 1: Check response handling after revalidation
After callingres.revalidate()and sending JSON, the function should return to avoid continuing execution.Step 2: Identify missing return statement
The code callsres.json()but does not return, which can cause errors or multiple responses.Final Answer:
Not returning after res.json() causing possible errors -> Option BQuick Check:
Always return after sending response [OK]
- Using GET instead of POST (allowed but not recommended)
- Forgetting to return after res.json()
- Assuming res.revalidate() rebuilds entire site
Solution
Step 1: Understand res.revalidate usage
Theres.revalidate()method accepts a single path string and returns a promise.Step 2: Revalidate multiple pages sequentially
To revalidate multiple pages, callawait res.revalidate()separately for each path before sending response.Final Answer:
await res.revalidate('/about'); await res.revalidate('/contact'); res.json({ revalidated: true }) -> Option AQuick Check:
Call res.revalidate() separately for each page [OK]
- Passing array of paths to res.revalidate() (not supported)
- Calling res.revalidate without await causing race conditions
- Calling res.revalidate without returning or sending response
