dioxus/packages/ssr/README.md

106 lines
3.5 KiB
Markdown
Raw Normal View History

<div align="center">
<h1>Dioxus Server-Side Rendering (SSR)</h1>
<p>
<strong>Render Dioxus to valid html.</strong>
</p>
</div>
## Resources
2023-09-16 17:03:27 +00:00
This crate is a part of the broader Dioxus ecosystem. For more resources about Dioxus, check out:
- [Getting Started](https://dioxuslabs.com/learn/0.5/getting_started)
- [Book](https://dioxuslabs.com/learn/0.5/)
- [Examples](https://github.com/DioxusLabs/dioxus/tree/main/examples)
2021-09-25 02:15:50 +00:00
## Overview
Dioxus SSR provides utilities to render Dioxus components to valid HTML. Once rendered, the HTML can be rehydrated client-side or served from your web server of choice.
```rust
# use dioxus::prelude::*;
fn app() -> Element {
rsx!{
div {"hello world!"}
}
}
let mut vdom = VirtualDom::new(app);
2024-04-17 22:08:38 +00:00
vdom.rebuild_in_place();
let text = dioxus_ssr::render(&vdom);
2021-09-25 02:15:50 +00:00
assert_eq!(text, "<div>hello world!</div>")
```
## Basic Usage
2024-01-16 19:18:46 +00:00
The simplest example is to simply render some `rsx!` nodes to HTML. This can be done with the [`render_element`] API.
```rust, no_run
# use dioxus::prelude::*;
2024-01-16 19:18:46 +00:00
let content = dioxus_ssr::render_element(rsx!{
div {
for i in 0..5 {
"Number: {i}"
}
}
});
```
## Rendering a VirtualDom
```rust, no_run
# use dioxus::prelude::*;
# fn app() -> Element { todo!() }
let mut vdom = VirtualDom::new(app);
2024-04-17 22:08:38 +00:00
vdom.rebuild_in_place();
let content = dioxus_ssr::render(&vdom);
```
## Usage in pre-rendering
2021-09-25 02:15:50 +00:00
This crate is particularly useful in pre-generating pages server-side and then selectively loading Dioxus client-side to pick up the reactive elements.
2021-09-25 02:15:50 +00:00
2023-09-16 17:03:27 +00:00
This crate supports hydration out of the box. However, both the client and server must generate the _exact_ same VirtualDOMs - the client picks up its VirtualDOM assuming that the pre-rendered page output is the same. To do this, you need to make sure that your VirtualDOM implementation is deterministic! This could involve either serializing our app state and sending it to the client, hydrating only parts of the page, or building tests to ensure what's rendered on the server is the same as the client.
2021-09-25 02:15:50 +00:00
With pre-rendering enabled, this crate will generate element nodes with Element IDs pre-associated. During hydration, the Dioxus-WebSys renderer will attach the Virtual nodes to these real nodes after a page query.
To enable pre-rendering, simply set the pre-rendering flag to true.
2021-09-25 02:15:50 +00:00
```rust, no_run
# use dioxus::prelude::*;
# fn App() -> Element { todo!() }
let mut vdom = VirtualDom::new(App);
2024-04-17 22:08:38 +00:00
vdom.rebuild_in_place();
let mut renderer = dioxus_ssr::Renderer::new();
renderer.pre_render = true;
2021-09-25 02:15:50 +00:00
let text = renderer.render(&vdom);
2021-09-25 02:15:50 +00:00
```
## Usage in server-side rendering
Dioxus SSR can also be used to render on the server. You can just render the VirtualDOM to a string and send that to the client.
2021-09-25 02:15:50 +00:00
```rust
# use dioxus::prelude::*;
fn App() -> Element {
rsx! { div { "hello world!" } }
}
let mut vdom = VirtualDom::new(App);
vdom.rebuild_in_place();
let text = dioxus_ssr::render(&vdom);
assert_eq!(text, "<div>hello world!</div>")
```
The rest of the space - IE doing this more efficiently, caching the VirtualDom, etc, will all need to be a custom implementation for now.
2021-09-25 02:15:50 +00:00
## Usage in static site generation
2021-09-25 02:15:50 +00:00
Dioxus SSR is a powerful tool to generate static sites. Using Dioxus for static site generation _is_ a bit overkill, however. The new documentation generation library, Doxie, is essentially Dioxus SSR on steroids designed for static site generation with client-side hydration.
Again, simply render the VirtualDOM to a string using `render` or any of the other render methods.