Enabling SSR (Server-Side Rendering) in Astro lets your website generate pages on the server before sending them to the browser. This makes pages load faster and helps search engines understand your content better.
Enabling SSR in Astro
export const ssr = true;
---
export const ssr = true;
---
<html>
<body>
<h1>Hello from SSR!</h1>
</body>
</html>---
export const ssr = true;
const time = new Date().toLocaleTimeString();
---
<html>
<body>
<p>Current server time: {time}</p>
</body>
</html>This Astro page enables SSR and renders a greeting message on the server. When you visit this page, the HTML is fully generated on the server and sent to your browser ready to display.
--- export const ssr = true; const greeting = 'Welcome to SSR with Astro!'; --- <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>SSR Example</title> </head> <body> <h1>{greeting}</h1> </body> </html>
Remember to restart your Astro dev server after changing SSR settings to see the effect.
SSR can increase server load, so use it when you need dynamic content or SEO benefits.
Combine SSR with client-side hydration if you want interactive components after the page loads.
Adding export const ssr = true; in an Astro file enables server-side rendering.
SSR helps pages load faster and improves SEO by sending ready HTML from the server.
Use SSR when your page needs fresh data or better search engine visibility.
Practice
export const ssr = true; in an Astro component do?Solution
Step 1: Understand the purpose of
Settingssrin Astroexport const ssr = true;tells Astro to render the component on the server before sending it to the browser.Step 2: Compare with other options
The other options describe disabling or client-only behavior, which is not whatssr = truedoes.Final Answer:
Enables server-side rendering for that component -> Option DQuick Check:
SSR enabled = export const ssr = true [OK]
- Thinking ssr disables JavaScript
- Confusing SSR with client-only rendering
- Assuming ssr=true stops rendering
Solution
Step 1: Identify correct syntax for SSR in Astro
The correct syntax is to export a constant namedssrwith valuetrueto enable SSR.Step 2: Check each option
export const ssr = false; disables SSR. const ssr = true; missesexport. enableSSR(true); is not valid Astro syntax.Final Answer:
export const ssr = true; -> Option AQuick Check:
Correct SSR syntax = export const ssr = true [OK]
- Forgetting to export the ssr constant
- Using a function call instead of export
- Setting ssr to false by mistake
--- export const ssr = true; const time = new Date().toISOString(); ---Current time: {time}
What will the user see when they load this page?
Solution
Step 1: Understand SSR effect on data
Withssr = true, the page is rendered on the server at request time, sonew Date()runs fresh each request.Step 2: Compare options
The current time when the page was built (static time) is static build time (no SSR). An error becausessrcannot be true is false; SSR=true is valid. No time displayed because SSR disables variables is incorrect; SSR does not disable variables.Final Answer:
The current time when the page was requested (fresh time) -> Option AQuick Check:
SSR renders fresh data on each request [OK]
- Thinking SSR uses build time data
- Assuming SSR disables JavaScript variables
- Confusing SSR with static site generation
--- ssr = true; ---Hello world
Why does this cause an error?
Solution
Step 1: Check correct SSR syntax
SSR must be enabled by exporting a constant:export const ssr = true;. Just assigningssr = true;is invalid.Step 2: Analyze error cause
Withoutexport const, Astro does not recognize the SSR flag, causing an error.Final Answer:
Becausessrmust be exported withexport const-> Option CQuick Check:
SSR requires export const ssr = true [OK]
- Forgetting to export ssr
- Setting ssr without const
- Trying to set ssr inside HTML
--- // Fill in the missing code ---User Profile
Name: {user.name}
Age: {user.age}
Solution
Step 1: Enable SSR correctly
Useexport const ssr = true;to enable server-side rendering so data is fresh on each request.Step 2: Fetch data asynchronously
Useawaitto fetch user data asynchronously before rendering the page.Step 3: Analyze options
export const ssr = false; const user = await fetchUserFromAPI(); disables SSR, so data won't be fresh. const ssr = true; const user = fetchUserFromAPI(); misses export and await. export const ssr = true; const user = fetchUserFromAPI(); misses await, so user is a Promise, causing error.Final Answer:
export const ssr = true; const user = await fetchUserFromAPI(); -> Option BQuick Check:
SSR + await fetch = fresh data per request [OK]
- Not exporting ssr constant
- Forgetting to await async data fetch
- Disabling SSR by setting ssr to false
Ready to prove it?
Pass the test and this pattern moves unknown
