What if your website could always show the freshest info without confusing users or tricky hacks?
Why Opting out of caching in NextJS? - Purpose & Use Cases
Imagine you have a website that shows live sports scores. You try to update the page, but it keeps showing old scores because the browser or server saved a copy and keeps reusing it.
Manually managing when to refresh or not refresh data is tricky. Cached pages can show outdated info, confusing users. Constantly clearing cache or adding special tricks is slow and error-prone.
Next.js lets you easily tell the system when NOT to cache a page or data. This way, your page always shows fresh info without complicated hacks.
res.setHeader('Cache-Control', 'max-age=3600') // caches for 1 hour
export const revalidate = 0 // disables caching, always freshYou can build pages that always show the latest data, perfect for live updates or sensitive info.
A news site showing breaking news that must update instantly without users seeing old headlines.
Manual cache control is hard and error-prone.
Next.js provides simple ways to opt out of caching.
This ensures users always see fresh, accurate data.
Practice
Solution
Step 1: Understand Next.js caching control
Next.js uses therevalidateexport to control caching duration for static pages.Step 2: Disable caching by setting
Settingrevalidateto zerorevalidate = 0tells Next.js to never cache and always fetch fresh data.Final Answer:
Setexport const revalidate = 0;in the page file -> Option DQuick Check:
revalidate = 0 disables caching [OK]
- Confusing revalidate with cache settings in config
- Trying to disable caching via headers only
- Omitting the export keyword for revalidate
Solution
Step 1: Check correct export syntax
In Next.js, caching control requires exporting a constant namedrevalidate.Step 2: Use number zero without quotes
The value must be a number 0, not a string, and must be exported properly.Final Answer:
export const revalidate = 0; -> Option BQuick Check:
Correct export with number zero disables caching [OK]
- Using string '0' instead of number 0
- Not exporting the variable
- Using default export incorrectly
export const revalidate = 0;
export default function Page() {
return <h1>Fresh Content</h1>;
}Solution
Step 1: Identify the revalidate value
The code setsrevalidate = 0, which disables caching.Step 2: Understand effect on page behavior
With caching disabled, the page always fetches fresh content on each request.Final Answer:
The page is never cached and always fetches fresh content -> Option AQuick Check:
revalidate = 0 means no caching [OK]
- Assuming default caching applies
- Thinking 0 means cache forever
- Confusing with ISR default timing
const revalidate = 0;
export default function Page() {
return <div>No Cache</div>;
}What is the error?
Solution
Step 1: Check revalidate declaration
The variablerevalidatemust be exported to affect caching.Step 2: Identify missing export
Withoutexport, Next.js ignores the variable and uses default caching.Final Answer:
Missing export keyword for revalidate -> Option AQuick Check:
revalidate must be exported to disable caching [OK]
- Forgetting to export revalidate
- Thinking revalidate must be non-zero
- Confusing functional and class components
Solution
Step 1: Understand conditional caching needs
Disabling caching only for authenticated users requires dynamic rendering based on request context.Step 2: Use dynamic rendering with auth check
Settingexport const dynamic = 'force-dynamic';forces server-side rendering, allowing auth checks per request.Step 3: Why other options fail
Setexport const revalidate = 0;unconditionally in the page file disables caching for all users; Setrevalidate = 0insidegetStaticPropsconditionally is invalid becauserevalidateis static; AddCache-Control: no-storeheader in_app.jsfor all requests affects all requests globally.Final Answer:
Use server-side rendering withexport const dynamic = 'force-dynamic';and check auth inside -> Option CQuick Check:
Dynamic rendering enables conditional caching [OK]
- Trying to set revalidate conditionally inside getStaticProps
- Disabling caching globally instead of per user
- Misusing cache headers in _app.js
Ready to prove it?
Pass the test and this pattern moves unknown
