0
0
NextJSframework~20 mins

On-demand revalidation in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
On-demand Revalidation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when on-demand revalidation is triggered in Next.js?

Consider a Next.js page using getStaticProps with revalidate set. When you call res.revalidate('/path') in an API route, what is the immediate effect on the page?

NextJS
export async function getStaticProps() {
  return {
    props: { time: Date.now() },
    revalidate: 10
  };
}

export default function Page({ time }) {
  return <p>Time: {time}</p>;
}

// API route handler
export default async function handler(req, res) {
  await res.revalidate('/path');
  res.status(200).json({ revalidated: true });
}
AThe page is rebuilt in the background and the next request to the page gets the updated content.
BThe page is immediately rebuilt and the new content is served on the next request.
CThe page is rebuilt immediately and the client currently viewing the page sees the updated content instantly.
DNothing happens until the revalidate time expires; the API call has no effect.
Attempts:
2 left
💡 Hint

Think about how Next.js handles static regeneration and when the new content becomes available.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly triggers on-demand revalidation in Next.js API route?

Choose the correct way to trigger on-demand revalidation for the path /blog/post-1 inside a Next.js API route.

NextJS
export default async function handler(req, res) {
  // Your code here
}
Aawait res.revalidate('/blog/post-1'); res.json({ done: true });
Bawait res.revalidate('/blog/post-1'); res.send({ done: true });
Cawait res.revalidate('/blog/post-1'); res.status(200).json({ done: true });
Dres.revalidate('/blog/post-1'); res.status(200).json({ done: true });
Attempts:
2 left
💡 Hint

Remember to await the revalidation and send a proper HTTP status with JSON response.

🔧 Debug
advanced
2:00remaining
Why does on-demand revalidation fail with a 500 error in this code?

Given this API route code, why does calling it cause a 500 error?

NextJS
export default async function handler(req, res) {
  try {
    await res.revalidate('/');
    res.status(200).json({ revalidated: true });
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
}
AThe API route is missing the <code>secret</code> query parameter required for revalidation.
BThe <code>res.revalidate</code> method requires the full URL, not just the path.
CThe <code>res.revalidate</code> method is not supported in API routes and causes an error.
DThe <code>res.json</code> call is missing a status code, causing the error.
Attempts:
2 left
💡 Hint

Check Next.js docs about securing on-demand revalidation.

state_output
advanced
2:00remaining
What is the output after on-demand revalidation updates the page?

Assume a Next.js page shows the current timestamp from getStaticProps with revalidate: 60. After calling res.revalidate('/time') in an API route, what will the page show on the next request?

NextJS
export async function getStaticProps() {
  return {
    props: { time: Date.now() },
    revalidate: 60
  };
}

export default function TimePage({ time }) {
  return <p>Timestamp: {time}</p>;
}
AAn error because the page cannot update until the revalidate time expires.
BThe timestamp from the API route that called res.revalidate, instantly updated.
CThe timestamp from the initial build, unchanged until 60 seconds pass.
DThe timestamp from the last successful on-demand revalidation call.
Attempts:
2 left
💡 Hint

Think about when the page props update after on-demand revalidation.

🧠 Conceptual
expert
2:00remaining
Which statement about on-demand revalidation in Next.js is TRUE?

Choose the correct statement about how on-demand revalidation works in Next.js.

AOn-demand revalidation disables the <code>revalidate</code> time set in <code>getStaticProps</code>.
BOn-demand revalidation requires a secret token to prevent unauthorized rebuilds.
COn-demand revalidation immediately updates all cached pages globally without waiting for requests.
DOn-demand revalidation can be triggered from client-side code directly without an API route.
Attempts:
2 left
💡 Hint

Consider security implications of rebuilding static pages on demand.