Q17: How do you opt out of caching for dynamic routes?
To force dynamic, real-time rendering on every request:
- Pass
{ cache: 'no-store' }or{ next: { revalidate: 0 } }tofetch(). - Export route segment configs:
export const dynamic = 'force-dynamic'. - Use dynamic functions like
cookies(),headers(), orsearchParams.
Force Dynamic Route Config
// app/live-feed/page.js
export const dynamic = 'force-dynamic'; // Bypasses Full Route Cache
export default async function LiveFeed() {
const data = await fetch('https://api.com/feed').then(r => r.json());
return <div>Live Items: {data.length}</div>;
}💡 force-dynamic ensures the page renders on every incoming request on the server.
Key Takeaways
- Calling
cookies()orheaders()automatically converts a static route to dynamic rendering. export const dynamic = 'force-dynamic'forces SSR across the entire page.
Knowledge Check
What happens to a static page route when you call cookies() or headers() inside its Server Component?