Performance: Cache invalidation strategies
This affects page load speed by controlling how fresh content is served and how often the browser or server must fetch new data.
export async function getStaticProps() { const data = await fetch('https://api.example.com/data').then(res => res.json()); return { props: { data }, revalidate: 60 } // Revalidate every 60 seconds }
export async function getStaticProps() { const data = await fetch('https://api.example.com/data').then(res => res.json()); return { props: { data } }; } // No revalidation or cache control, content can become stale indefinitely
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No cache invalidation | Minimal | 0 | Low but stale content delays perceived load | [X] Bad |
| ISR with revalidate | Minimal | 0 | Low with fresh content | [OK] Good |
| Client fetch without cache | Minimal | 0 | Blocks interaction due to network delay | [X] Bad |
| Client fetch with cache | Minimal | 0 | Fast response improves interaction | [OK] Good |
| Static assets no cache headers | N/A | N/A | High due to repeated downloads | [X] Bad |
| Static assets with long cache | N/A | N/A | Low, instant load on repeat visits | [OK] Good |
getStaticProps generates static pages at build time, but with revalidate, it can update pages after a set interval.getServerSideProps fetches fresh data on every request but does not use caching or revalidation intervals.getStaticProps with revalidate -> Option DgetStaticProps + revalidate [OK]getServerSideProps with revalidationgetStaticProps?revalidate as the property to set seconds for incremental static regeneration.getStaticProps must include props and optionally revalidate with a number value.revalidate [OK]export async function getStaticProps() {
return {
props: { time: Date.now() },
revalidate: 5
};
}revalidate: 5 means Next.js will regenerate the page at most every 5 seconds.export async function getStaticProps() {
return {
props: { data: await fetchData() },
revalidate: '10'
};
}revalidaterevalidate property expects a number representing seconds, not a string.revalidate must be a number, not a string -> Option AgetServerSideProps runs on the server for every page request, ensuring fresh data.getStaticProps generates static pages at build time or with revalidation intervals, so it does not guarantee fresh data every request.getServerSideProps because it runs on every request -> Option CgetServerSideProps [OK]Pass the test and this pattern moves unknown