📖 Scenario: You are building a simple blog homepage using Next.js. The blog posts data changes occasionally, so you want to make sure the page updates without rebuilding the entire site every time.
🎯 Goal: Create a Next.js page that fetches blog posts data and uses revalidate to update the page every 10 seconds automatically.
📋 What You'll Learn
Create a posts array with three blog post objects, each having id, title, and content fields.
Add a revalidateTime variable set to 10 (seconds).
Write an async function getStaticProps that returns the posts data and sets revalidate to revalidateTime.
Create a default exported React functional component HomePage that receives posts as props and renders the titles in an unordered list.
💡 Why This Matters
🌍 Real World
Many websites need to update content regularly without full rebuilds. Next.js revalidation lets you keep pages fresh with minimal server load.
💼 Career
Understanding revalidation patterns is important for building fast, scalable React apps with Next.js that handle dynamic data efficiently.
Progress0 / 4 steps
1
Create the blog posts data array
Create a constant array called posts with exactly three objects. Each object must have these exact keys and values: { id: 1, title: 'First Post', content: 'This is the first post.' }, { id: 2, title: 'Second Post', content: 'This is the second post.' }, and { id: 3, title: 'Third Post', content: 'This is the third post.' }.
NextJS
Hint
Use const posts = [ ... ] with three objects inside.
2
Add the revalidation time variable
Create a constant called revalidateTime and set it to the number 10.
NextJS
Hint
Use const revalidateTime = 10; to set the revalidation interval.
3
Write the getStaticProps function with revalidation
Write an async function called getStaticProps that returns an object with props containing the posts array, and a revalidate property set to revalidateTime.
NextJS
Hint
Use export async function getStaticProps() { return { props: { posts }, revalidate: revalidateTime } }.
4
Create the HomePage component to display posts
Create a default exported React functional component called HomePage that accepts posts as a prop. Inside, return a <main> element containing an unordered list <ul>. Map over posts and render each post's title inside a <li> with a unique key set to the post's id.
NextJS
Hint
Use export default function HomePage({ posts }) { return ( <main><ul>{posts.map(post => <li key={post.id}>{post.title}</li>)}</ul></main> ) }.
Practice
(1/5)
1. What does the revalidate export in Next.js control?
easy
A. How often a cached page updates after initial generation
B. The maximum number of pages to cache
C. The delay before a page is first generated
D. The priority of server-side rendering
Solution
Step 1: Understand the role of revalidate
The revalidate export sets the time in seconds for Next.js to update a cached page after it was first generated.
Step 2: Compare options with this behavior
Only How often a cached page updates after initial generation correctly describes this update frequency control. Other options describe unrelated concepts.
Final Answer:
How often a cached page updates after initial generation -> Option A
Quick Check:
Revalidate controls update frequency = D [OK]
Hint: Remember revalidate sets cache update time in seconds [OK]
Common Mistakes:
Confusing revalidate with cache size
Thinking revalidate delays first page generation
Mixing revalidate with rendering priority
2. Which of the following is the correct syntax to set a page to revalidate every 60 seconds in Next.js?
easy
A. export revalidate = 60;
B. export const revalidate = 60;
C. const revalidate = 60 export;
D. export const revalidate(60);
Solution
Step 1: Recall correct export syntax in Next.js
To export a constant, the syntax is export const name = value;.
Step 2: Match this with revalidate usage
export const revalidate = 60; matches this pattern exactly. Other options have syntax errors or misplaced keywords.
Final Answer:
export const revalidate = 60; -> Option B
Quick Check:
Correct export syntax = A [OK]
Hint: Use 'export const' to export revalidate value [OK]