Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
On-demand Revalidation in Next.js
📖 Scenario: You are building a blog with Next.js. You want to update the blog posts page when new posts are added without rebuilding the whole site manually.
🎯 Goal: Create a Next.js page that shows a list of blog posts. Add an API route that triggers on-demand revalidation to update the page when new posts are added.
📋 What You'll Learn
Create a Next.js page at app/posts/page.tsx that fetches and displays a list of posts.
Create a mock data array called posts with exact post titles.
Add a configuration variable revalidatePath with the path to revalidate.
Create an API route at app/api/revalidate/route.ts that calls Next.js revalidatePath to refresh the posts page.
Use export const revalidate = 0 in the posts page to enable on-demand revalidation.
💡 Why This Matters
🌍 Real World
On-demand revalidation lets websites update static pages instantly when content changes, without rebuilding the whole site. This is useful for blogs, news sites, and e-commerce.
💼 Career
Understanding on-demand revalidation is important for Next.js developers to build fast, dynamic sites that stay up-to-date with minimal delay.
Progress0 / 4 steps
1
Create the posts data array
Create a constant array called posts in app/posts/page.tsx with these exact strings: 'First Post', 'Second Post', and 'Third Post'.
NextJS
Hint
Use const posts = ['First Post', 'Second Post', 'Third Post'];
2
Add the revalidate path variable
Add a constant called revalidatePath and set it to the string '/posts' in app/posts/page.tsx.
NextJS
Hint
Write const revalidatePath = '/posts';
3
Create the posts page with revalidation export
In app/posts/page.tsx, export const revalidate = 0 to enable on-demand revalidation. Then create a default React component called PostsPage that returns a <main> element with an <h1> titled 'Blog Posts' and an unordered list <ul> showing each post from the posts array inside <li> elements.
NextJS
Hint
Use export const revalidate = 0; and map over posts inside the component.
4
Create the API route for on-demand revalidation
Create a file app/api/revalidate/route.ts. Inside, export an async function called GET that accepts a Request parameter. Inside the function, call await res.revalidate(revalidatePath) to trigger revalidation of the /posts page. Then return a JSON response with { revalidated: true }. Import NextResponse from next/server and use it to return the JSON response.
NextJS
Hint
Use export async function GET(request: Request) and call await res.revalidate(revalidatePath).
Practice
(1/5)
1. What is the main purpose of on-demand revalidation in Next.js?
easy
A. To disable static site generation completely
B. To rebuild the whole site every time a user visits
C. To update specific static pages instantly without rebuilding the entire site
D. To cache pages permanently without updates
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 C
Quick Check:
On-demand revalidation = update specific pages fast [OK]
Hint: Remember: on-demand means update only what is needed [OK]
Common Mistakes:
Thinking it rebuilds the entire site
Confusing it with disabling static generation
Assuming pages never update
2. Which Next.js API method is used inside an API route to trigger on-demand revalidation?
easy
A. res.reload(path)
B. res.refresh(path)
C. res.update(path)
D. res.revalidate(path)
Solution
Step 1: Identify the method for revalidation
Next.js provides res.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 D
Quick Check:
API route uses res.revalidate() to update pages [OK]
Hint: Look for 'revalidate' keyword in the method name [OK]
Common Mistakes:
Using non-existent methods like res.refresh()
Confusing with client-side reload methods
Misspelling the method name
3. Given this API route code snippet, what will happen when a POST request with the correct secret is sent?
B. Not returning after res.json() causing possible errors
C. Using GET method instead of POST for revalidation
D. Incorrect path string in res.revalidate()
Solution
Step 1: Check response handling after revalidation
After calling res.revalidate() and sending JSON, the function should return to avoid continuing execution.
Step 2: Identify missing return statement
The code calls res.json() but does not return, which can cause errors or multiple responses.
Final Answer:
Not returning after res.json() causing possible errors -> Option B
Quick Check:
Always return after sending response [OK]
Hint: Return immediately after sending response [OK]
Common Mistakes:
Using GET instead of POST (allowed but not recommended)
Forgetting to return after res.json()
Assuming res.revalidate() rebuilds entire site
5. You want to revalidate multiple pages on-demand after updating content. Which approach correctly triggers revalidation for '/about' and '/contact' in one API route?
hard
A. await res.revalidate('/about'); await res.revalidate('/contact'); res.json({ revalidated: true })
B. await res.revalidate(['/about', '/contact']); res.json({ revalidated: true })
C. res.revalidate('/about', '/contact'); res.json({ revalidated: true })
D. res.revalidate('/about'); res.revalidate('/contact'); res.json({ revalidated: true })
Solution
Step 1: Understand res.revalidate usage
The res.revalidate() method accepts a single path string and returns a promise.
Step 2: Revalidate multiple pages sequentially
To revalidate multiple pages, call await res.revalidate() separately for each path before sending response.