Invalidate the cache on demand
On-demand revalidation lets you refresh a cached page the moment its data changes (for example after a CMS update) instead of waiting for the scheduled revalidate window to expire.
When to reach for it: best paired with a CMS or backend that updates content on a webhook. See How ISR works for how invalidation regenerates and re-caches a page.
Preconditions
- A working ISR setup with an
ISRHandlerinstance and aninvalidateSecretTokenconfigured. - A JSON body parser on the server (
express.json()).
Steps
Expose an invalidate endpoint. Wire a
POSTroute toISRHandler.invalidate. Make sure the JSON body parser is registered so the token and URLs can be read.server.tsexport function app(): express.Express {
// ...other configuration
server.use(express.json());
server.post('/api/invalidate', (req, res) => isr.invalidate(req, res));
return server;
}Send a POST request to
/api/invalidatewith the secret token and the URLs to invalidate.{
"token": "your-secret-token",
"urlsToInvalidate": ["/", "/docs/on-demand-revalidation"]
}tokenmust match theinvalidateSecretTokenset on theISRHandlerconfig;urlsToInvalidateis the list of URLs to regenerate.
Set revalidate: 0 on a route to disable scheduled (automatic) revalidation entirely. The cache then only refreshes when you invalidate it on demand.
Result
Each URL in urlsToInvalidate (and its variants) is regenerated and re-cached immediately, and invalidate returns a JSON summary of what was invalidated, not in the cache, or errored. Content stays fresh without waiting for the revalidate window.
See also
- Reference:
ISRHandler.invalidate·InvalidateConfig - Reference:
invalidateSecretTokenandrevalidatesemantics - How-to: Serve cache variants