Performance: Why caching is central to Next.js
HIGH IMPACT
Caching in Next.js affects page load speed and reduces server response time by reusing previously generated content.
export async function getStaticProps() { const data = await fetch('https://api.example.com/data'); const json = await data.json(); return { props: { json }, revalidate: 60 }; } // Static generation with incremental static regeneration caches page for 60 seconds
export async function getServerSideProps() { const data = await fetch('https://api.example.com/data'); const json = await data.json(); return { props: { json } }; } // This runs on every request, no caching
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Server-side rendering on every request | N/A | N/A | High due to slow server response | [X] Bad |
| Static generation with caching | N/A | N/A | Low due to fast server response | [OK] Good |