Concept Flow - Opting out of caching
Request received
Check cache policy
Cache
Send response
When a request comes in, Next.js checks if caching is allowed. If opted out, it fetches fresh data every time.
export const dynamic = 'force-dynamic'; export default function Page() { return <h1>Fresh content every time</h1>; }
| Step | Action | Cache Policy | Data Source | Response Sent |
|---|---|---|---|---|
| 1 | Request received | Check dynamic export | N/A | N/A |
| 2 | dynamic = 'force-dynamic' detected | No caching | Fetch fresh data | N/A |
| 3 | Render page with fresh data | No caching | Fresh data | <h1>Fresh content every time</h1> |
| 4 | Send response to client | No caching | Fresh data | <h1>Fresh content every time</h1> |
| 5 | Next request arrives | Repeat steps 1-4 | Always fresh | Always fresh response |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| dynamic | undefined | 'force-dynamic' | 'force-dynamic' | 'force-dynamic' | 'force-dynamic' |
| cacheAllowed | true | false | false | false | false |
| dataSource | N/A | fresh fetch | fresh data | fresh data | fresh data |
| responseContent | N/A | N/A | <h1>Fresh content every time</h1> | <h1>Fresh content every time</h1> | <h1>Fresh content every time</h1> |
Next.js lets you control caching with the dynamic export. Set dynamic = 'force-dynamic' to disable caching. This makes Next.js fetch fresh data every request. Useful for pages needing always up-to-date content. Without it, Next.js may cache and reuse data. Remember: disabling cache can affect performance.
revalidate export to control caching duration for static pages.revalidate to zerorevalidate = 0 tells Next.js to never cache and always fetch fresh data.export const revalidate = 0; in the page file -> Option Drevalidate.export const revalidate = 0;
export default function Page() {
return <h1>Fresh Content</h1>;
}revalidate = 0, which disables caching.const revalidate = 0;
export default function Page() {
return <div>No Cache</div>;
}revalidate must be exported to affect caching.export, Next.js ignores the variable and uses default caching.export const dynamic = 'force-dynamic'; forces server-side rendering, allowing auth checks per request.export const revalidate = 0; unconditionally in the page file disables caching for all users; Set revalidate = 0 inside getStaticProps conditionally is invalid because revalidate is static; Add Cache-Control: no-store header in _app.js for all requests affects all requests globally.export const dynamic = 'force-dynamic'; and check auth inside -> Option CPass the test and this pattern moves unknown