0
0
NextJSframework~5 mins

Opting out of caching in NextJS

Choose your learning style9 modes available
Introduction

Sometimes you want your web page to always show fresh data. Opting out of caching helps you do that by telling Next.js not to save the page content for later.

When showing live data like stock prices or sports scores that change often.
When displaying user-specific information that should not be shared or stored.
When debugging and you want to see changes immediately without cached versions.
When your page content depends on real-time events or external APIs that update frequently.
Syntax
NextJS
export const revalidate = 0;

Setting revalidate to 0 tells Next.js to never cache the page.

This is used in server components or page files to control caching behavior.

Examples
This page will never be cached and always fetch fresh data on every request.
NextJS
export const revalidate = 0;

export default function Page() {
  return <p>Always fresh content</p>;
}
This page caches content for 10 seconds before refreshing.
NextJS
export const revalidate = 10;

export default function Page() {
  return <p>Content updates every 10 seconds</p>;
}
Sample Program

This Next.js page shows the current time and never caches it, so it updates on every request.

NextJS
export const revalidate = 0;

export default function LiveTime() {
  const time = new Date().toLocaleTimeString();
  return <p>Current time: {time}</p>;
}
OutputSuccess
Important Notes

Opting out of caching can increase server load because pages are generated on every request.

Use this only when you really need fresh data to avoid slowing down your site.

Summary

Set revalidate = 0 to disable caching in Next.js pages.

This ensures the page content is always fresh and up-to-date.

Use it carefully to balance freshness and performance.