Course contentsShow
← Web Development
Lesson 1797 of 2,98233. Static Site Generation and JamstackFree lesson

On-Demand Revalidation

Triggering immediate page regeneration via API routes or webhooks when content changes without waiting for intervals.

On-Demand Revalidation

What you'll learn: How to instantly rebuild static pages when content changes, without waiting for timed intervals.

The Problem with Interval-Based Updates

In the previous lesson, you configured pages to revalidate every N seconds (revalidate: 60). This works, but creates a gap: when you publish a new blog post at 2:00 PM, visitors might see stale content until the next interval hits. What if you could regenerate pages immediately when content changes?

How On-Demand Revalidation Works

On-demand revalidation lets you trigger page rebuilds programmatically via API routes or webhooks, bypassing the time-based interval. Think of it like a doorbell: instead of checking every 5 minutes if someone's at the door (interval), the visitor rings and you know instantly (on-demand).

When your CMS publishes new content, it sends a webhook to your site. Your API route receives this, calls a revalidation function for specific paths, and those pages regenerate right away. Fresh content appears in seconds, not minutes.

The Pattern

You create an API endpoint that validates the request (often using a secret token) and calls revalidate() with specific page paths:

/api/revalidate
  ↓
Check secret token
  ↓
Trigger rebuild for /blog/new-post
  ↓
Return success

Your headless CMS webhook points to this endpoint. When an editor clicks "Publish," the webhook fires, your API route validates it, and the page regenerates immediately.

Why This Matters

You get the best of both worlds: the performance and scalability of static pages, with the freshness of dynamic content. No more waiting for intervals. No manual rebuilds. Content updates propagate instantly while keeping infrastructure costs low.

Key Takeaway:
On-demand revalidation triggers immediate page regeneration via API routes or webhooks, giving you instant content updates without sacrificing static generation benefits.