Static vs SSR vs Hybrid in Astro: Key Differences and Usage
Static rendering generates HTML at build time for fast loading, SSR (Server-Side Rendering) creates HTML on each request for dynamic content, and Hybrid combines both by selectively using static and SSR pages in one project. This lets you optimize performance and interactivity based on your app's needs.Quick Comparison
Here is a quick overview comparing Static, SSR, and Hybrid rendering modes in Astro.
| Factor | Static | SSR (Server-Side Rendering) | Hybrid |
|---|---|---|---|
| Rendering Time | At build time | On each request | Mix of build time and request time |
| Performance | Very fast load, no server delay | Slower due to server processing | Optimized per page |
| Content Freshness | Fixed at build | Always fresh | Fresh where SSR used |
| Use Case | Mostly static sites, blogs | Dynamic apps, user-specific data | Sites with both static and dynamic needs |
| Server Requirement | No server needed after build | Server required to render pages | Server needed only for SSR parts |
| Complexity | Simplest | More complex setup | Moderate complexity |
Key Differences
Static rendering in Astro means all HTML pages are generated once during the build process. This makes pages load very fast because they are just plain files served by any web server or CDN. However, the content cannot change until you rebuild the site.
SSR (Server-Side Rendering) generates HTML on-demand for each user request. This allows dynamic content that changes frequently or depends on user data. It requires a server to run the rendering logic every time a page is requested, which can slow down response time compared to static pages.
Hybrid mode lets you mix both approaches in one Astro project. You can choose which pages are static and which use SSR. This gives you the best of both worlds: fast static pages where possible and dynamic SSR pages where needed. It requires careful planning but offers great flexibility and performance optimization.
Static Rendering Code Example
This Astro component generates a static page at build time with a fixed message.
--- const message = 'Hello from Static Astro!'; --- <html lang="en"> <head> <title>Static Page</title> </head> <body> <h1>{message}</h1> </body> </html>
SSR Equivalent Code Example
This Astro component uses server-side rendering to show the current server time on each request.
--- export async function get() { return { body: JSON.stringify({ time: new Date().toISOString() }) }; } --- <html lang="en"> <head> <title>SSR Page</title> </head> <body> <h1>Current Server Time: {Astro.props.time}</h1> </body> </html>
When to Use Which
Choose Static when your site content rarely changes and you want the fastest load times with minimal server costs, like blogs or documentation.
Choose SSR when your pages need to show fresh or user-specific data on every request, such as dashboards or personalized apps.
Choose Hybrid when your project has a mix of static and dynamic pages, allowing you to optimize performance and flexibility by using static rendering where possible and SSR only where necessary.