What are Server Islands?
Server Islands are components that render on the server for each request, even when the rest of the page is statically generated. They load asynchronously after the initial page render, providing dynamic content without blocking the main page load.Server Islands require an SSR adapter and work in
output: 'static' or output: 'hybrid' modes.Creating a Server Island
Mark any component as a server island using theserver:defer directive:
src/pages/index.astro
Server Island Component
Server islands are just regular Astro components that can access server-side features:src/components/UserProfile.astro
How It Works
1
Initial Page Load
The static HTML is served immediately with a placeholder for the server island.
2
Server Rendering
The browser requests the server island content via a background fetch:
3
Hydration
The server responds with rendered HTML, which replaces the placeholder:
Performance Impact
Performance Impact
- Initial page loads instantly (static HTML)
- Server islands load asynchronously
- No blocking on server-rendered content
- Main content visible immediately
Fallback Content
Fallback Content
Provide fallback content that shows while loading:
Use Cases
Personalization
Show user-specific content like profiles, recommendations, or preferences
Real-time Data
Display live data like stock prices, availability, or analytics
Authentication
Render auth-protected content based on session state
A/B Testing
Serve different variants for experiments
Passing Props
Pass props to server islands just like regular components:src/pages/product/[id].astro
src/components/StockStatus.astro
Slots
Use slots to pass content to server islands:src/pages/dashboard.astro
src/components/AdminPanel.astro
Configuration
Enable server islands in your Astro config:astro.config.mjs
Combining with Client Islands
Mix server islands with client-side framework components:src/pages/app.astro
Server Islands
Server Islands
- Render on server for each request
- Access databases and sessions
- Load asynchronously
- No JavaScript hydration needed
Client Islands
Client Islands
- Render on client
- Interactive components
- Require JavaScript
- Hydrate in the browser
Caching Strategies
Control caching for server islands:src/components/CachedWidget.astro
server:defer:
Error Handling
Handle errors gracefully in server islands:src/components/WeatherWidget.astro
Loading States
Provide meaningful loading states:src/pages/index.astro
Advanced Example
A complete example combining multiple concepts:src/pages/dashboard.astro
Best Practices
1
Keep islands small
Server islands should be focused components. Split large components into multiple islands.
2
Provide fallbacks
Always show something while loading. Use skeleton screens or loading messages.
3
Handle errors
Gracefully handle failures and provide retry mechanisms.
4
Consider caching
Cache server island responses when appropriate to reduce server load.
5
Monitor performance
Track server island response times and optimize slow queries.
Related Resources
SSR and SSG
Understanding rendering modes
Components
Building Astro components
Islands Architecture
Learn about islands architecture
Middleware
Add server-side logic