Hybrid rendering lets you build web pages that combine fast static parts with dynamic server parts. This helps your site load quickly but still show fresh data.
Hybrid rendering (static + server) in Astro
--- // Astro component frontmatter const data = await fetch('https://api.example.com/data').then(res => res.json()); --- <html> <body> <h1>Static content here</h1> <p>Dynamic data: {data.value}</p> </body> </html>
Use the frontmatter (---) to fetch server data asynchronously.
Static parts are written normally in the template outside the frontmatter.
---
const time = new Date().toLocaleTimeString();
---
<html>
<body>
<h1>Static title</h1>
<p>Current time: {time}</p>
</body>
</html>--- const posts = await fetch('https://api.example.com/posts').then(r => r.json()); --- <html> <body> <h1>Blog Posts</h1> <ul> {posts.map(post => <li>{post.title}</li>)} </ul> </body> </html>
This Astro component fetches user data on the server and inserts the user's name into an otherwise static page. The header and footer stay the same for all visitors, making the page fast and efficient.
--- const user = await fetch('https://api.example.com/user').then(res => res.json()); --- <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Hybrid Rendering Example</title> </head> <body> <header> <h1>Welcome to Our Site</h1> </header> <main> <p>Hello, {user.name}!</p> <p>This page is mostly static but shows your name dynamically.</p> </main> <footer> <small>Ā© 2024 Example Corp</small> </footer> </body> </html>
Hybrid rendering improves performance by serving static content quickly.
Use server code in frontmatter to fetch fresh data for dynamic parts.
Remember to keep static and dynamic parts clearly separated for best results.
Hybrid rendering mixes static and server-rendered content for speed and freshness.
Use Astro frontmatter to fetch data on the server side.
This approach is great for pages that mostly stay the same but need some live info.
Practice
Solution
Step 1: Understand hybrid rendering purpose
Hybrid rendering mixes static and server-rendered content to get the best of both worlds: speed and fresh data.Step 2: Compare options
Only 'It combines fast static content with fresh server data.' correctly describes this benefit. The other options misunderstand hybrid rendering.Final Answer:
It combines fast static content with fresh server data. -> Option BQuick Check:
Hybrid rendering = fast + fresh [OK]
- Thinking hybrid disables server rendering
- Assuming hybrid is client-only rendering
- Believing hybrid needs no data fetching
Solution
Step 1: Identify Astro frontmatter usage
Astro uses frontmatter (---) to run server-side code before rendering.Step 2: Check options for server-side fetch
--- const data = await fetch('https://api.example.com') ---uses frontmatter with await fetch, which is correct. The other options lack frontmatter or use client-side hooks.Final Answer:
---\nconst data = await fetch('https://api.example.com')\n--- -> Option CQuick Check:
Server fetch = frontmatter + await [OK]
- Omitting frontmatter for server code
- Using client hooks like useEffect in Astro
- Not awaiting fetch in server code
---
const time = new Date().toLocaleTimeString();
---
Current Time:
{time}
Solution
Step 1: Understand Astro frontmatter timing
Code inside frontmatter runs once at build or server render time, so time is fixed then.Step 2: Check rendering behavior
The rendered page shows the time captured during rendering, not live updates. So The time when the page was built (static time). is correct.Final Answer:
The time when the page was built (static time). -> Option AQuick Check:
Frontmatter time = fixed render time [OK]
- Thinking time updates live on client
- Confusing Astro syntax with React JSX
- Assuming {time} causes error
---
const data = fetch('https://api.example.com/data');
---
{data.json()}
Solution
Step 1: Check fetch usage
fetch returns a Promise; without await, data is a Promise, not the response.Step 2: Understand rendering error
Calling data.json() on a Promise causes an error because data is not resolved yet.Final Answer:
fetch is not awaited, so data is a Promise, causing an error. -> Option AQuick Check:
Always await fetch in frontmatter [OK]
- Forgetting to await fetch
- Misunderstanding frontmatter fetch rules
- Thinking data.json() returns string directly
Solution
Step 1: Separate static and dynamic data
Posts are mostly static, so fetching them in frontmatter for static build is efficient.Step 2: Use server component for live data
Visitor count changes often, so fetching it in a server component ensures fresh data on each request.Step 3: Evaluate options
Fetch posts in frontmatter for static build, then fetch visitor count inside a server component. correctly mixes static and server rendering. The other options do not use hybrid rendering properly.Final Answer:
Fetch posts in frontmatter for static build, then fetch visitor count inside a server component. -> Option DQuick Check:
Static + server fetch = hybrid rendering [OK]
- Fetching all data statically losing freshness
- Fetching static data on client unnecessarily
- Not separating static and dynamic concerns
Ready to prove it?
Pass the test and this pattern moves unknown
