CSR vs SSR vs SSG
CSR vs SSR vs SSG
Three main rendering approaches for web applications.
Client-Side Rendering (CSR)
Server → Empty HTML + JavaScript → Browser renders
// React SPA (CSR)
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}
Pros:
- Simple deployment
- Rich interactions
- Better caching
Cons:
- Slow initial load
- Poor SEO
- Blank screen while loading
Server-Side Rendering (SSR)
Server → Rendered HTML → Browser hydrates
// Next.js SSR
export async function getServerSideProps() {
const data = await fetchProducts();
return { props: { data } };
}
export default function Products({ data }) {
return <ProductList products={data} />;
}
Pros:
- Fast initial load
- Good SEO
- Works without JavaScript
Cons:
- Server load
- Complex deployment
- Slower TTFB
Static Site Generation (SSG)
Build time → Static HTML → Served from CDN
// Next.js SSG
export async function getStaticProps() {
const data = await fetchProducts();
return { props: { data } };
}
export default function Products({ data }) {
return <ProductList products={data} />;
}
Pros:
- Fastest performance
- Great SEO
- Easy to scale
Cons:
- Build time generation
- Not for real-time data
- Rebuild needed for updates
Choosing a Strategy
Choosing a Strategy
Decision Matrix
| Content Type | Strategy | Example |
|---|---|---|
| Static marketing | SSG | Landing pages |
| Blog posts | SSG/ISR | Documentation |
| User dashboard | CSR | Admin panels |
| Product pages | SSR/ISR | E-commerce |
| Real-time data | CSR | Stock prices |
| Personalized | SSR | User profiles |
When to Use Each
Use CSR when:
- App is behind authentication
- Content is highly dynamic
- SEO doesn't matter
- Rich interactions needed
Use SSR when:
- SEO is critical
- Content changes frequently
- Personalization needed
- First load performance matters
Use SSG when:
- Content is mostly static
- Performance is critical
- High traffic expected
- Content changes infrequently
Hybrid Approaches
// Next.js: Mix SSR and SSG
// Static pages (SSG)
export async function getStaticProps() {
return { props: { data: await fetchStaticData() } };
}
// Dynamic pages (SSR)
export async function getServerSideProps(context) {
return { props: { data: await fetchDynamicData(context.params.id) } };
}
// Incremental Static Regeneration (ISR)
export async function getStaticProps() {
return {
props: { data: await fetchProducts() },
revalidate: 60, // Regenerate every 60 seconds
};
}
Hybrid Rendering
Hybrid Rendering
Combine rendering strategies for optimal performance.
Next.js Hybrid Approach
// pages/products/[id].js
// Static generation for most products
export async function getStaticProps({ params }) {
const product = await fetchProduct(params.id);
return {
props: { product },
revalidate: 3600, // ISR: update every hour
};
}
export async function getStaticPaths() {
const products = await fetchPopularProducts();
return {
paths: products.map(p => ({ params: { id: p.id } })),
fallback: 'blocking', // SSR for non-pre-rendered paths
};
}
Route-Based Strategy
// Different strategies per route
const routes = {
'/': { strategy: 'SSG', revalidate: 3600 },
'/products': { strategy: 'SSR' },
'/products/[id]': { strategy: 'ISR', revalidate: 60 },
'/dashboard': { strategy: 'CSR' },
'/blog/[slug]': { strategy: 'SSG', revalidate: 86400 },
};
Component-Level Rendering
// Different components, different strategies
function ProductPage({ product }) {
return (
<div>
{/* Static header */}
<Header />
{/* SSR product info */}
<ProductInfo product={product} />
{/* CSR reviews (real-time) */}
<Suspense fallback={<ReviewsSkeleton />}>
<DynamicReviews productId={product.id} />
</Suspense>
{/* Static footer */}
<Footer />
</div>
);
}
Benefits of Hybrid Rendering
- Best of all worlds: SSR for SEO, CSR for interactivity
- Optimized performance: Each page uses optimal strategy
- Flexibility: Change strategy per route
- Cost efficiency: Static pages served from CDN
Frameworks
- Next.js: React (SSG, SSR, ISR)
- Nuxt.js: Vue (SSG, SSR)
- Astro: Multi-framework (Islands)
- Gatsby: React (SSG with plugins)
Practice Problems
Create a reusable React component implementing Rendering Strategies. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for Rendering Strategies using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize Rendering Strategies for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What is the main advantage of CSR?
2. When should you use SSG?
3. What is the primary purpose of Rendering Strategies?
4. What is a common mistake when implementing Rendering Strategies?
Flashcards
Question
What is CSR?
Click to reveal answer
Answer
Client-Side Rendering - browser renders the page using JavaScript.
Question
What is SSR?
Click to reveal answer
Answer
Server-Side Rendering - server generates HTML that browser hydrates with JavaScript.
Question
What is SSG?
Click to reveal answer
Answer
Static Site Generation - HTML is generated at build time and served from CDN.
Question
What is hybrid rendering?
Click to reveal answer
Answer
Combining CSR, SSR, and SSG strategies based on route or component needs.
Question
What is Rendering Strategies?
Click to reveal answer
Answer
Rendering Strategies is a key concept in frontend development.
Revision Notes
Key Takeaways
- 1.CSR is best for interactive apps behind authentication
- 2.SSR is best for SEO-critical dynamic content
- 3.SSG is best for static content needing fast performance
- 4.ISR combines SSG performance with dynamic updates
- 5.Hybrid rendering lets you choose the best strategy per route
Interview Tips
- •Explain the trade-offs between CSR, SSR, and SSG
- •Discuss when to use each rendering strategy
- •Know how ISR works and its benefits
Cheat Sheet
Rendering Strategies Cheat Sheet
CSR
- Browser renders with JavaScript
- Good for: Dashboards, apps behind auth
- Bad for: SEO, first load
SSR
- Server renders HTML, browser hydrates
- Good for: SEO, dynamic content
- Bad for: Server load
SSG
- Build time HTML, CDN delivery
- Good for: Static content, performance
- Bad for: Real-time data
ISR
- SSG with background regeneration
- Best of SSG + freshness