What is Hydration?
When a page is server-side rendered:- Server: Renders component tree to HTML and embeds state
- Browser: Displays static HTML immediately
- WASM loads: Client code downloads and initializes
- Hydration: Client “hydrates” the DOM, attaching interactivity
How Hydration Works
Server-Side
- Render Components: VirtualDom renders to HTML
- Collect State: Hooks like
use_signal,use_server_futureserialize their data - Embed Data: State embedded in HTML as base64-encoded CBOR
- Add IDs: Elements get
data-node-idattributes
Client-Side
- Parse Hydration Data: Read
window.__DIOXUS_HYDRATION_DATA__ - Create VirtualDom: Build same component tree
- Restore State: Hooks read cached values
- Match Nodes: Map virtual nodes to real DOM
- Attach Handlers: Add event listeners
Hydration Context
Dioxus automatically manages aHydrationContext that stores serialized state:
Hydration-Aware Hooks
Several hooks are designed for hydration:use_server_future
Runs on server, caches result for client:
- Future executes
- Result serialized to hydration data
- HTML rendered with result
- Future doesn’t execute
- Result read from hydration data
- No server request needed
use_server_cached
Cache computed values:
use_signal with Initial Value
Signals hydrate their initial values:
Hydration Mismatches
Hydration fails if server and client render differently:Common Causes
❌ Random valuesSolutions
✅ Use effects for client-only codeControlling Hydration
Skip Hydration for Elements
Some content doesn’t need hydration:Conditional Rendering
Handle differences between server and client:Testing Hydration
Verify Deterministic Rendering
Ensure server and client produce identical output:Manual Hydration Test
Performance Considerations
Hydration Data Size
Large state increases HTML size. Minimize hydration data:Hydration Timing
Hydration blocks interactivity. Optimize by:- Reduce JavaScript size: Smaller WASM = faster hydration
- Use code splitting: Load only what’s needed
- Defer non-critical hydration: Use
use_effectfor delayed work
Debugging Hydration
Enable Logging
Dioxus logs hydration events:Inspect Hydration Data
View hydration data in the browser console:Check Node IDs
Inspectdata-node-id attributes in the rendered HTML to verify hydration markers are present.
Advanced Patterns
Partial Hydration
Hydrate only interactive parts:Progressive Hydration
Hydrate in priority order:Streaming with Hydration
Stream HTML while hydrating earlier parts:examples/07-fullstack/streaming.rs.
Best Practices
- Keep rendering deterministic - same inputs produce same outputs
- Use
use_effectfor client-only code - browser APIs, timers, randomness - Minimize hydration data - only serialize what’s needed
- Test both server and client - ensure consistent rendering
- Use server futures wisely - cache expensive operations
- Handle hydration errors gracefully - provide fallbacks
- Monitor hydration performance - use browser dev tools
Common Issues
Hydration Mismatch Errors
- Random values
- Current time
- Browser APIs used during initial render
Missing Hydration Data
Slow Hydration
Solution:- Reduce WASM bundle size
- Split code
- Defer non-critical hydration
- Use streaming SSR
Examples
examples/07-fullstack/fullstack_hello_world.rs- Basic hydrationexamples/07-fullstack/server_functions.rs- Server data hydrationexamples/playwright-tests/fullstack-hydration-order- Hydration order testsexamples/07-fullstack/streaming.rs- Streaming with hydration