Overview
Server-Side Rendering (SSR) allows you to generate pages dynamically on each request using Vercel Edge Functions. This enables real-time data, personalized content, and access to request context like headers and geolocation.SSR pages are executed at the edge, close to your users, providing fast response times while maintaining dynamic capabilities.
Enabling SSR
To enable SSR for a page, setprerender to false in the page’s frontmatter:
src/pages/ssr.astro
Accessing Request Headers
SSR pages have access to the full request context, including headers. This is useful for personalization based on geolocation, user agent, or custom headers:src/pages/index.astro
Available Request Headers
Vercel Edge Network Headers
x-real-ip- The client’s IP addressx-vercel-ip-city- The city of the clientx-vercel-ip-country- The country code of the clientx-vercel-ip-latitude- The latitude of the clientx-vercel-ip-longitude- The longitude of the client
Performance Benefits
- Edge Execution
- Dynamic Content
- Scalability
SSR pages run on Vercel’s Edge Network, which means:
- Execution close to users worldwide
- Low latency responses
- Global distribution without additional configuration
Configuration
SSR is enabled in your Astro config with the Vercel adapter:astro.config.mjs
SSR with Caching
You can combine SSR with cache-control headers to cache responses at the edge while still regenerating them server-side. This provides a balance between dynamic content and performance:src/pages/ssr-with-swr-caching.astro
The
s-maxage=10 directive caches the response for 10 seconds, while stale-while-revalidate allows serving stale content while fetching fresh data in the background.Use Cases
- Personalization: Display content based on user location, preferences, or authentication
- Real-time Data: Show live prices, availability, or status updates
- A/B Testing: Serve different variants based on request parameters
- Authentication: Check session cookies and protect routes
- Dynamic API Responses: Create endpoints that return fresh data on each request
Next Steps
ISR
Combine static generation with on-demand revalidation
Edge Functions
Build API endpoints with Edge Functions