On-demand revalidation lets your Next.js app update specific pages without rebuilding the whole site. It keeps content fresh and fast.
On-demand revalidation in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
import { NextApiRequest, NextApiResponse } from 'next'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { // Check secret token for security if (req.query.secret !== process.env.MY_SECRET_TOKEN) { return res.status(401).json({ message: 'Invalid token' }); } try { // Revalidate the page at the given path await res.revalidate('/path-to-revalidate'); return res.json({ revalidated: true }); } catch (err) { return res.status(500).send('Error revalidating'); } }
You create an API route that calls res.revalidate(path) to refresh a page.
Use a secret token to protect the API route from unauthorized calls.
export default async function handler(req, res) { if (req.query.secret !== process.env.MY_SECRET) { return res.status(401).json({ message: 'Unauthorized' }); } await res.revalidate('/blog/post-1'); return res.json({ revalidated: true }); }
export default async function handler(req, res) { const slug = req.query.slug; if (!slug || req.query.secret !== process.env.MY_SECRET) { return res.status(401).json({ message: 'Unauthorized' }); } await res.revalidate(`/products/${slug}`); return res.json({ revalidated: true }); }
This API route revalidates the home page when called with the correct secret token. It helps update the home page content without a full rebuild.
import { NextApiRequest, NextApiResponse } from 'next'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.query.secret !== process.env.MY_SECRET_TOKEN) { return res.status(401).json({ message: 'Invalid token' }); } try { await res.revalidate('/'); return res.json({ revalidated: true }); } catch (err) { return res.status(500).send('Error revalidating'); } }
Always protect your revalidation API route with a secret token to avoid misuse.
On-demand revalidation works only with pages using getStaticProps and revalidate set.
Use this method to keep static pages fresh without slowing down your site.
On-demand revalidation updates specific pages instantly without rebuilding the whole site.
It uses an API route that calls res.revalidate(path) with a secret token for security.
This keeps your static pages fresh and fast 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
