β‘ SSR vs SSG vs ISR in React (Next.js)
πΉ 1. SSR (Server-Side Rendering)
Section titled βπΉ 1. SSR (Server-Side Rendering)β- Page is render on server for every request.
- Always fresh data, but slower than static.
- β Use case: Pages with personalized / frequently changing data (e.g., dashboards, logged-in profile, admin pages).
- Example :
export async function getServerSideProps() { const res = await fetch("https://api.example.com/data"); const data = await res.json(); return { props: { data } }; }- π Each request β Next.js runs
getServerSidePropsβ HTML generated β sent to browser.
πΉ 2. SSG (Static Site Generation)
Section titled βπΉ 2. SSG (Static Site Generation)β- Page is built at compile time (build step).
- Super fast (just HTML + JS served from CDN).
- Data is frozen at build time.
- β Use case: Pages with rarely changing data (e.g., blog posts, docs, landing pages).
- Example :
export async function getStaticProps() { const res = await fetch("https://api.example.com/data"); const data = await res.json(); return { props: { data } }; }- π Runs once during build β HTML + JSON cached on CDN β instant load.
πΉ 3. ISR (Incremental Static Regeneration)
Section titled βπΉ 3. ISR (Incremental Static Regeneration)β- Hybrid between SSG + SSR.
- Page is built at built time, but can be re-generated in the background after a set time.
- Users always get fast static page, but data updates periodically.
- β Use case: Pages with semi-dynamic data (e.g., product listings, news site).
- Example :
export async function getStaticProps() { const res = await fetch("https://api.example.com/data"); const data = await res.json(); return { props: { data }, revalidate: 60, // re-generate every 60s }; }- π First request β old cached page served.
- π Background β page regenerated.
- π Next user β gets updated page.
βοΈ Comparison Table
Section titled ββοΈ Comparison Tableβ| Feature | SSR (Server-Side) | SSG (Static Site) | ISR (Incremental Static) |
|---|---|---|---|
| Render timing | Every request | At build time | At build + revalidated later |
| Performance | β Slower (server work) | β Fast (CDN cached) | β Fast (CDN cached + refresh) |
| Data freshness | β Always fresh | β Stale if data changes | β‘ Semi-fresh (updates on interval) |
| Use cases | Dashboards, auth pages | Blogs, docs, marketing | Product pages, news feeds |
π§ Interview One-liners
Section titled βπ§ Interview One-linersβ- βSSR fetches fresh data on each request but is slower.β
- βSSG pre-builds pages at compile time for maximum speed, but content is frozen until rebuild.β
- βISR combines both: pages are cached like static but periodically refreshed in the background.β