Leptos Hydrated
A lightweight library for flicker-free interactive state hydration in Leptos 0.8.
This library provides primitives to synchronize state from the server to the client seamlessly, ensuring that the initial render on the client matches the server-rendered HTML exactly, preventing "flashes" of default state before the client-side hydration completes.
Features
- Flicker-Free: Initializes signals with server-provided state immediately during hydration.
- Isomorphic: Works naturally in both SSR and CSR contexts.
- Context Integration: Provide global hydrated state via
HydrateContext. - Scoped Hydration: Use the
Hydratecomponent for localized hydrated state. - Resource Support: Automatically manages a background
Resourceto keep data in sync.
Installation
Add this to your Cargo.toml:
[]
# Once published to crates.io
= "0.1.0"
# Or via git
# leptos_hydrated = { git = "https://github.com/incon/leptos_hydrated" }
= { = "1.0", = ["derive"] }
Quick Start
1. Define your State
2. Wrap your App in HydrateContext
Use ssr_value to provide the immediate synchronous state (e.g., from cookies) and fetcher for the full asynchronous data load.
3. Use the state in components
Best Practices: Specialized Contexts
For larger applications, it is a best practice to wrap HydrateContext in specialized components. This keeps your App component clean and allows you to place contexts exactly where they are needed.
// In your App
view!
Example Project
A full demonstration is available in the examples/hydrate_showcase directory. It features:
- Dark/Light mode synchronization via cookies.
- Authentication state persistence.
- URL Parameter synchronization with hydrated state.
To run the example: