What is SSR?
SSR renders your components to HTML strings on the server:SSR vs CSR
Client-Side Rendering (CSR)
- Browser downloads JavaScript/WASM
- JavaScript executes
- Components render
- User sees content
Server-Side Rendering (SSR)
- Server renders HTML
- Browser displays HTML immediately
- JavaScript/WASM downloads in background
- Hydration attaches interactivity
Comparison
| Feature | CSR | SSR |
|---|---|---|
| Initial Load | Slow | Fast |
| SEO | Poor | Good |
| Server Required | No | Yes |
| Time to Interactive | Same as visible | Slower than visible |
| Complexity | Low | Medium |
Basic Setup
Dioxus handles SSR automatically when usingdioxus::launch:
Feature Flags
YourCargo.toml needs the right features:
Rendering HTML
Automatic Rendering
Withdioxus::launch, SSR happens automatically on the server side.
Manual Rendering
For custom setups, render manually:Rendering Elements
Render individualrsx! blocks without a VirtualDom:
Custom Server Setup
For more control, usedioxus::serve with custom Axum configuration:
SSR Configuration
Configure SSR behavior withServeConfig:
Custom HTML Template
Provide a custom HTML wrapper:{app} placeholder is replaced with your rendered component.
Data Fetching
Fetch data on the server before rendering:Server-Side Data Loading
use_server_future hook:
- Runs on the server during SSR
- Serializes the result into the HTML
- On the client, reads the cached result
- Prevents duplicate requests
Initial Props
Pass data to your component from the server:Performance Optimization
Rendering Pool
Dioxus uses a pool of renderers for concurrent requests. Configure pool size:Incremental Rendering
For large pages, render incrementally:Caching
Cache rendered pages:Suspense and Streaming
Render pages before all data is ready using Suspense boundaries:Streaming Mode
Enable out-of-order streaming to send initial HTML immediately:- Server sends initial HTML with loading placeholders
- Client displays immediately
- As data loads, server streams updates
- Client patches in the real content
examples/07-fullstack/streaming.rs for details.
SEO Optimization
Meta Tags
Set page metadata:Structured Data
Add JSON-LD for rich results:Error Handling
Error Boundaries
Catch rendering errors:Custom Error Pages
Handle HTTP errors:examples/07-fullstack/custom_error_page.rs.
Testing SSR
Test server-rendered output:Deployment
Deploy your SSR app as a standard Rust binary:0.0.0.0:8080 by default. Configure with environment variables:
- Docker
- Fly.io
- Railway
- AWS
- Digital Ocean
SSR-Only Mode
Build server-rendered apps without client-side JavaScript:- Content sites
- Admin panels
- Progressive enhancement scenarios
examples/07-fullstack/ssr-only.
Best Practices
- Use
use_server_futurefor data that should load on the server - Implement Suspense for better loading states
- Add meta tags for SEO
- Cache rendered pages when possible
- Test both server and client rendering to ensure consistency
- Use error boundaries to handle rendering failures gracefully
- Keep components deterministic - same input should produce same output
Common Issues
Hydration Mismatches
Server and client must render identically:Server-Only Code
Keep server-only code behind feature flags:Examples
examples/07-fullstack/fullstack_hello_world.rs- Basic SSR setupexamples/07-fullstack/custom_axum_serve.rs- Custom serverexamples/07-fullstack/ssr-only/- SSR without hydrationexamples/07-fullstack/streaming.rs- Streaming SSR