Performance: Why SSR enables dynamic content
This affects the initial page load speed and perceived content freshness by rendering dynamic content on the server before sending HTML to the browser.
export async function get() { const data = await fetchDataFromAPI(); return { body: JSON.stringify(data) }; } const Component = ({data}) => { return <p>{data.message}</p>; };
const Component = () => {
const [data, setData] = React.useState(null);
React.useEffect(() => {
fetch('/api/data').then(res => res.json()).then(setData);
}, []);
if (!data) return <p>Loading...</p>;
return <p>{data.message}</p>;
};| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side fetch after load | Adds nodes after initial load | Triggers multiple reflows | High paint cost due to delayed content | [X] Bad |
| Server-Side Rendering dynamic content | DOM ready on load | Single reflow on initial paint | Low paint cost with immediate content | [OK] Good |
export const prerender = false; to enable server-side rendering.export const prerender = false; [OK]export const prerender = false;
const time = new Date().toLocaleTimeString();
---
<div>Current time: {time}</div>new Date() is fresh every time.export const prerender = false;
const userName = fetchUserName();
---
<div>Hello, {userName}!</div>fetchUserName() is asynchronous but used without await. -> Option Cexport const prerender = false;
async function getUserName() {
return await fetch('/api/user').then(res => res.json()).then(data => data.name);
}
const userName = await getUserName();
---
<h1>Welcome back, {userName}!</h1>Pass the test and this pattern moves unknown