Revalidation patterns help keep your website data fresh by updating content after it is first loaded. This means users see up-to-date information without waiting for a full reload.
Revalidation patterns in NextJS
export const revalidate = 60; // seconds export default async function Page() { const data = await fetch('https://api.example.com/data').then(res => res.json()); return <main>{JSON.stringify(data)}</main>; }
The revalidate export sets how often Next.js updates the page in seconds.
Using cache: 'no-store' in fetch disables caching for fresh data on each request.
export const revalidate = 10; export default async function Page() { const data = await fetch('https://api.example.com/posts').then(res => res.json()); return <div>{data.length} posts loaded</div>; }
revalidate to 0 disables caching and fetches fresh data on every request.export const revalidate = 0; export default async function Page() { const data = await fetch('https://api.example.com/data', { cache: 'no-store' }).then(res => res.json()); return <pre>{JSON.stringify(data, null, 2)}</pre>; }
export const revalidate = 3600; export default async function Page() { const data = await fetch('https://api.example.com/stats').then(res => res.json()); return <p>Stats updated hourly: {data.count}</p>; }
This Next.js page fetches a todo item from an API and shows it. The page content updates every 30 seconds to keep data fresh without full reloads.
export const revalidate = 30; export default async function Page() { const response = await fetch('https://jsonplaceholder.typicode.com/todos/1'); const todo = await response.json(); return ( <main> <h1>Todo Item</h1> <p><strong>ID:</strong> {todo.id}</p> <p><strong>Title:</strong> {todo.title}</p> <p><strong>Completed:</strong> {todo.completed ? 'Yes' : 'No'}</p> <p><em>Page revalidates every 30 seconds.</em></p> </main> ); }
Revalidation helps balance fast loading and fresh data.
Set revalidate to 0 for always fresh data but slower loads.
Use longer revalidation times for rarely changing data to save resources.
Revalidation patterns let Next.js update cached pages after a set time.
Use export const revalidate = seconds; to control update frequency.
This improves user experience by showing fresh data with good performance.
Practice
revalidate export in Next.js control?Solution
Step 1: Understand the role of
Therevalidaterevalidateexport 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 AQuick Check:
Revalidate controls update frequency = D [OK]
- Confusing revalidate with cache size
- Thinking revalidate delays first page generation
- Mixing revalidate with rendering priority
Solution
Step 1: Recall correct export syntax in Next.js
To export a constant, the syntax isexport 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 BQuick Check:
Correct export syntax = A [OK]
- Omitting 'const' keyword
- Using parentheses incorrectly
- Placing export keyword after variable
export const revalidate = 10;
export default function Page() {
return <div>Time: {Date.now()}</div>;
}What will happen if you visit this page multiple times within 10 seconds?
Solution
Step 1: Understand revalidate timing effect
Withrevalidate = 10, Next.js caches the page and only regenerates it after 10 seconds.Step 2: Predict page behavior on multiple visits
Within 10 seconds, the cached page is served, so the timestamp stays the same. After 10 seconds, it updates.Final Answer:
The page shows the same timestamp for 10 seconds before updating -> Option DQuick Check:
Revalidate caches page for 10 seconds = B [OK]
- Assuming Date.now() updates on every visit
- Thinking revalidate disables page updates
- Confusing revalidate with client-side rendering
export const revalidate = '30';
export default function Page() {
return <div>Hello</div>;
}What is the problem with this code?
Solution
Step 1: Check the type of revalidate value
Therevalidateexport expects a number representing seconds, not a string.Step 2: Identify the error caused by wrong type
Using a string will cause Next.js to ignore or error on revalidation timing.Final Answer:
revalidate must be a number, not a string -> Option AQuick Check:
revalidate requires number type = A [OK]
- Using quotes around the number
- Thinking revalidate needs async function
- Misunderstanding export rules
export const revalidate = ?;
Choose the best value and explain why.
Solution
Step 1: Convert 5 minutes to seconds
5 minutes equals 300 seconds, so revalidate should be set to 300.Step 2: Understand caching vs freshness tradeoff
Setting revalidate to 300 caches the page for 5 minutes, improving performance but still updating regularly.Step 3: Evaluate other options
0 disables caching (no performance gain), 60 updates too often, Infinity never updates (stale data).Final Answer:
300, because 300 seconds equals 5 minutes, balancing freshness and caching -> Option CQuick Check:
5 minutes = 300 seconds revalidate [OK]
- Using 0 thinking it improves performance
- Choosing too short or too long revalidate values
- Confusing minutes with seconds
Ready to prove it?
Pass the test and this pattern moves unknown
