📖 Scenario: You are building a simple Astro website that needs to show the current server time dynamically. To do this, you will enable Server-Side Rendering (SSR) in Astro and create a component that displays the server time on each page load.
🎯 Goal: Enable SSR in an Astro project and create a component that shows the current server time rendered on the server.
📋 What You'll Learn
Create an Astro configuration file with SSR enabled
Create a component that fetches the current server time
Use the component in a page to display the server-rendered time
Ensure the Astro project is set up for SSR rendering
💡 Why This Matters
🌍 Real World
Many websites need to show dynamic data that updates on each page load, like current time, user info, or live stats. SSR helps generate this content on the server before sending it to the browser.
💼 Career
Understanding SSR in Astro is useful for frontend developers working with modern static site generators and frameworks that support server rendering for better performance and SEO.
Progress0 / 4 steps
1
Create astro.config.mjs with SSR enabled
Create a file named astro.config.mjs and write the code to enable SSR by exporting a default configuration object with output set to 'server'.
Astro
Hint
Use export default { output: 'server' }; to enable SSR in Astro.
2
Create a component ServerTime.astro to get current server time
Create a new component file named ServerTime.astro. Inside, write frontmatter code to get the current date and time using new Date() and display it inside a <p> tag with the text Server time: followed by the date string.
Astro
Hint
Use frontmatter with const now = new Date() and display it inside a paragraph.
3
Use ServerTime component in the main page src/pages/index.astro
In src/pages/index.astro, import the ServerTime component and include it inside the page's markup so the server time is displayed on the homepage.
Astro
Hint
Import the component with import ServerTime from '../components/ServerTime.astro' and use <ServerTime /> in the page.
4
Complete SSR setup by ensuring project structure and config are correct
Verify that astro.config.mjs has output: 'server' and that the ServerTime.astro component is used in src/pages/index.astro. Add any missing export or closing tags to complete the files.
Astro
Hint
Make sure all previous code pieces are combined and correct to enable SSR fully.
Practice
(1/5)
1. What does adding export const ssr = true; in an Astro component do?
easy
A. Turns off all rendering for the component
B. Disables client-side JavaScript for the component
C. Makes the component load only on the client
D. Enables server-side rendering for that component
Solution
Step 1: Understand the purpose of ssr in Astro
Setting export 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 what ssr = true does.
Final Answer:
Enables server-side rendering for that component -> Option D
Quick Check:
SSR enabled = export const ssr = true [OK]
Hint: Remember: ssr=true means server renders HTML first [OK]
Common Mistakes:
Thinking ssr disables JavaScript
Confusing SSR with client-only rendering
Assuming ssr=true stops rendering
2. Which of the following is the correct way to enable SSR in an Astro component?
easy
A. export const ssr = true;
B. export const ssr = false;
C. const ssr = true;
D. enableSSR(true);
Solution
Step 1: Identify correct syntax for SSR in Astro
The correct syntax is to export a constant named ssr with value true to enable SSR.
Step 2: Check each option
export const ssr = false; disables SSR. const ssr = true; misses export. enableSSR(true); is not valid Astro syntax.
Final Answer:
export const ssr = true; -> Option A
Quick Check:
Correct SSR syntax = export const ssr = true [OK]
Hint: Look for 'export const ssr = true;' exactly [OK]
Common Mistakes:
Forgetting to export the ssr constant
Using a function call instead of export
Setting ssr to false by mistake
3. Given this Astro component code:
---
export const ssr = true;
const time = new Date().toISOString();
---
Current time: {time}
What will the user see when they load this page?
medium
A. The current time when the page was requested (fresh time)
B. The current time when the page was built (static time)
C. An error because ssr cannot be true
D. No time displayed because SSR disables variables
Solution
Step 1: Understand SSR effect on data
With ssr = true, the page is rendered on the server at request time, so new 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 because ssr cannot 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 A
Quick Check:
SSR renders fresh data on each request [OK]
Hint: SSR runs code fresh on server per request [OK]
Common Mistakes:
Thinking SSR uses build time data
Assuming SSR disables JavaScript variables
Confusing SSR with static site generation
4. You wrote this Astro component:
---
ssr = true;
---
Hello world
Why does this cause an error?
medium
A. Because ssr must be a function, not a variable
B. Because ssr cannot be set to true
C. Because ssr must be exported with export const
D. Because ssr must be set inside the HTML, not script
Solution
Step 1: Check correct SSR syntax
SSR must be enabled by exporting a constant: export const ssr = true;. Just assigning ssr = true; is invalid.
Step 2: Analyze error cause
Without export const, Astro does not recognize the SSR flag, causing an error.
Final Answer:
Because ssr must be exported with export const -> Option C
Quick Check:
SSR requires export const ssr = true [OK]
Hint: Always use 'export const ssr = true;' to enable SSR [OK]
Common Mistakes:
Forgetting to export ssr
Setting ssr without const
Trying to set ssr inside HTML
5. You want to create an Astro page that shows a user's profile with fresh data on every request. Which approach correctly enables SSR and fetches fresh data?
---
// Fill in the missing code
---
User Profile
Name: {user.name}
Age: {user.age}
hard
A. const ssr = true;
const user = fetchUserFromAPI();
B. export const ssr = true;
const user = await fetchUserFromAPI();
C. export const ssr = false;
const user = await fetchUserFromAPI();
D. export const ssr = true;
const user = fetchUserFromAPI();
Solution
Step 1: Enable SSR correctly
Use export const ssr = true; to enable server-side rendering so data is fresh on each request.
Step 2: Fetch data asynchronously
Use await to 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 B
Quick Check:
SSR + await fetch = fresh data per request [OK]
Hint: Use 'export const ssr = true;' and await data fetch [OK]
Common Mistakes:
Not exporting ssr constant
Forgetting to await async data fetch
Disabling SSR by setting ssr to false
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.