Next.js supports multiple rendering strategies per page. You choose a strategy by exporting specific functions from a page file.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/vercel/next.js/llms.txt
Use this file to discover all available pages before exploring further.
Static Generation (SSG)
With Static Generation, the page HTML is generated at build time. The HTML is reused on every request and can be cached by a CDN.Without data
Pages that do not exportgetStaticProps or getServerSideProps are automatically statically generated:
pages/about.tsx
With data
ExportgetStaticProps to fetch data at build time:
pages/blog.tsx
When to use Static Generation
Use Static Generation for content that can be built once and served to all users:- Marketing pages
- Blog posts
- Product listings
- Documentation
Server-side Rendering (SSR)
With Server-side Rendering, the page HTML is generated on every request. ExportgetServerSideProps to opt a page into SSR:
pages/dashboard.tsx
When to use SSR
Use SSR when a page:- Displays personalized data (user profile, session-based content).
- Depends on request-time information (cookies, headers, geolocation).
- Shows data that changes too frequently to benefit from caching.
SSR pages cannot be cached by a CDN at the page level. If you need caching, add
Cache-Control headers in getServerSideProps, or consider using ISR instead.Incremental Static Regeneration (ISR)
ISR lets you update static pages after build time without rebuilding the entire site. Add arevalidate field to the return value of getStaticProps:
pages/products/[id].tsx
revalidate: 60:
- The cached page is served immediately.
- If more than 60 seconds have passed since the last generation, Next.js regenerates the page in the background.
- The next request receives the freshly generated page.
On-demand revalidation
You can trigger revalidation from an API route:pages/api/revalidate.ts
In the App Router, use
revalidatePath and revalidateTag from next/cache for on-demand revalidation.Automatic Static Optimization
Next.js automatically determines whether a page can be statically generated. If a page does not exportgetServerSideProps or getInitialProps, it is treated as static.
The absence of blocking data requirements is what triggers this optimization.
Client-side rendering (CSR)
For pages or components where you do not need server-rendered HTML, fetch data on the client.Using useEffect
pages/index.tsx
Using SWR
SWR is the recommended library for client-side data fetching. It handles caching, revalidation, and more:pages/index.tsx
