Performance: Enabling SSR in Astro
Enabling SSR affects how quickly the main content appears by rendering HTML on the server before sending it to the browser.
---astro.config.mjs---
export default {
output: 'server'
};
---Page.astro---
---
const data = await fetchData();
---
<html>
<body>
<div>{data.content}</div>
</body>
</html>export default function Page() {
return (
<div>
<ClientOnlyComponent />
</div>
);
}
// ClientOnlyComponent fetches data and renders only on client side| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side only rendering | Many DOM nodes created after JS loads | Multiple reflows as content loads | High paint cost due to delayed content | [X] Bad |
| Server-side rendering (SSR) | DOM nodes created immediately from HTML | Single reflow on initial paint | Lower paint cost with immediate content | [OK] Good |
export const ssr = true; in an Astro component do?ssr in Astroexport const ssr = true; tells Astro to render the component on the server before sending it to the browser.ssr = true does.ssr with value true to enable SSR.export. enableSSR(true); is not valid Astro syntax.--- export const ssr = true; const time = new Date().toISOString(); ---Current time: {time}
ssr = true, the page is rendered on the server at request time, so new Date() runs fresh each request.ssr cannot be true is false; SSR=true is valid. No time displayed because SSR disables variables is incorrect; SSR does not disable variables.--- ssr = true; ---Hello world
export const ssr = true;. Just assigning ssr = true; is invalid.export const, Astro does not recognize the SSR flag, causing an error.ssr must be exported with export const -> Option C--- // Fill in the missing code ---User Profile
Name: {user.name}
Age: {user.age}
export const ssr = true; to enable server-side rendering so data is fresh on each request.await to fetch user data asynchronously before rendering the page.Pass the test and this pattern moves unknown