Web Fundamentals · Lesson 8 of 10 · 4 min
Frontend Architectures & Server-Side Rendering Performance
This is how you explain the trade-offs between a fast initial paint and a fast interactive app.
The Core Trade-off: LCP vs. TTI
In an interview, you must frame Server-Side Rendering (SSR) as a trade-off. You are choosing to optimize for a faster First Contentful Paint (FCP) and Largest Contentful Paint (LCP) at the cost of a potentially slower Time to Interactive (TTI).
Client-Side Rendering (CSR) sends a nearly empty HTML file. The browser waits for JavaScript to download, parse, and execute before rendering anything. This leads to a slow LCP but can result in a faster TTI once the app is running, as everything is managed on the client.
SSR does the opposite. The server sends a fully-formed HTML document, so the browser can paint pixels immediately. This gives you a great LCP score. The browser then downloads the same JavaScript, which must take over the static HTML in a process called hydration. This hydration work is expensive and blocks the main thread, delaying TTI.
Answering "When would you use SSR?" is a test of your architectural judgment. Start with the trade-off.
Good answer: "I'd use SSR for content-heavy sites like e-commerce or blogs where a fast initial load and SEO are critical. The goal is to get meaningful content to the user's screen as fast as possible, even if full interactivity is slightly delayed."
Bad answer: "For better performance." It's not universally better; it just shifts the performance bottleneck.
How Hydration Delays Interactivity
Hydration is the process where your client-side JavaScript attaches event listeners and state to the DOM that was rendered by the server. It's the step that turns static HTML into a dynamic application.
This is the primary performance cost of SSR. While the page looks ready, it can't respond to user input until hydration is complete. This is the "uncanny valley" of web performance: a page that is visible but not interactive. The work includes:
- Downloading, parsing, and executing your entire JS bundle.
- Rebuilding a virtual DOM in memory.
- Comparing the virtual DOM to the server-rendered HTML to ensure they match.
- Attaching event listeners to the DOM nodes.
The trap: A common failure mode is a "hydration mismatch." This happens when the HTML rendered on the server is different from what the client expects to render. Causes include using window on the server or rendering different content based on a timestamp. React will discard the server-rendered HTML and perform a full client-side render, destroying your SSR performance benefit.
The SSR Lifecycle in Code
This is a minimal, complete example using Express and React. It demonstrates rendering to a string, passing initial data, and hydrating on the client. Pay attention to the window.__INITIAL_DATA__ pattern; it's a classic technique to avoid refetching data on the client that the server already has.
// server.ts
import express from 'express';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { App, AppProps } from './src/App';
const app = express();
app.use(express.static('dist'));
app.get('/', (req, res) => {
// 1. Fetch data on the server. In a real app, this would be an API call.
const initialData: AppProps = { initialCount: 42 };
// 2. Render the React app to an HTML string.
const appHtml = ReactDOMServer.renderToString(<App {...initialData} />);
// 3. Embed the initial data in the HTML for the client to use.
const html = `
<!DOCTYPE html>
<html>
<head>
<title>SSR Demo</title>
<script>window.__INITIAL_DATA__ = ${JSON.stringify(initialData)}</script>
</head>
<body>
<div id="root">${appHtml}</div>
<script src="/client.js"></script>
</body>
</html>
`;
// 4. Send the complete HTML document.
res.send(html);
});
app.listen(3000, () => console.log('Server is running.'));
// src/App.tsx
import React, { useState } from 'react';
export interface AppProps {
initialCount: number;
}
export const App: React.FC<AppProps> = ({ initialCount }) => {
const [count, setCount] = useState(initialCount);
return (
<div>
<h1>Server-Rendered App</h1>
<p>Count: {count}</p>
<button onClick={() => setCount(c => c + 1)}>Increment</button>
</div>
);
};
// src/client.tsx
import React from 'react';
import ReactDOMClient from 'react-dom/client';
import { App, AppProps } from './App';
declare global {
interface Window { __INITIAL_DATA__?: AppProps; }
}
const container = document.getElementById('root');
const initialData = window.__INITIAL_DATA__;
if (container) {
// Use hydrateRoot to attach React to the existing server-rendered HTML.
ReactDOMClient.hydrateRoot(container, <App {...initialData} />);
}
Visualizing the Full Round-Trip
This flow shows every step from user request to full interactivity. The critical delay is between the browser painting the HTML (H) and the app becoming interactive (L), which is entirely dependent on the size and complexity of your JavaScript.
Modern Patterns to Reduce Hydration Cost
The industry's focus has shifted to minimizing the amount of JavaScript needed for hydration. This is the frontier of frontend performance and a key topic for senior interviews.
Streaming SSR: Instead of waiting for the entire page to be ready, the server sends HTML in chunks. The browser can render the shell (header, nav) immediately while waiting for slower parts (like a data-heavy widget). This improves perceived performance but doesn't reduce the total JS cost. React 18's renderToPipeableStream enables this.
Partial Hydration & Islands Architecture: These are the most effective solutions. The core idea is to not hydrate the entire application. You treat static components as pure HTML and only ship JavaScript for the interactive "islands" on the page.
Frameworks like Astro pioneered this. A page might be 90% static HTML with a few interactive islands like an image carousel or a "add to cart" button. Each island is a self-contained component with its own script, loaded and hydrated independently. This drastically reduces the amount of JS sent to the client, leading to a much faster TTI.
Try it
Set up a minimal SSR app like the one above. Intentionally create a hydration mismatch: in your App.tsx component, add if (typeof window !== 'undefined') { console.log("Client render!"); }. Observe the warning in your browser console. Then, fix it by ensuring the initial render output is identical on both server and client. This 15-minute exercise will solidify your understanding of hydration's biggest pitfall.
Next: We will cover how to set and enforce performance goals with Performance Budgets, RUM, and Continuous Monitoring.
Lock it in — apply this lesson under interview conditions.