In Astro's Server-Side Rendering (SSR), how is user-specific data typically handled to show dynamic content?
Think about where the data is fetched and how the HTML is generated for each user.
SSR runs on the server for each request, allowing it to fetch fresh, user-specific data and embed it directly into the HTML sent to the browser. This enables dynamic content tailored to each user.
Consider an Astro SSR page that shows the current time. What will the user see when they load the page?
export async function get({ params }) { const currentTime = new Date().toLocaleTimeString(); return { body: `<p>The current time is: ${currentTime}</p>` }; }
Remember SSR runs on the server before sending the page.
Since SSR runs on the server, the time is captured when the server generates the page. The user sees this server time embedded in the HTML.
Which code snippet correctly fetches data during SSR in Astro to render dynamic content?
Check for proper use of async/await and returning the correct object.
Option A correctly awaits the fetch and json parsing, then returns an object with a body string. Other options either miss await or return wrong types.
This Astro SSR page always shows the same data even after the source changes. What is the likely cause?
export async function get() { const data = await fetch('https://api.example.com/data'); const json = await data.json(); return { body: `<p>${json.message}</p>`, headers: { 'cache-control': 'max-age=3600' } }; }
Look at the headers controlling caching behavior.
The cache-control header tells the server or CDN to cache the page for 3600 seconds (1 hour), so the user sees stale data until cache expires.
An Astro SSR page renders a counter starting at 0. The client JavaScript increments it to 5 after loading. What will the user see if they reload the page?
export async function get() { return { body: `<div id="counter">0</div>` }; } // Client script increments counter to 5 after load
Think about where the initial HTML comes from and how client scripts update it.
SSR generates the initial HTML with counter 0 on every request. Client JavaScript updates it after loading, but this state is not saved on the server, so reload resets it.
