dioxus/packages/ssr
Evan Almloff 34bdcd15cf
Switch to a pool of dynamic values for hot reloading (#2705)
* create the dynamic value pool

* assign ids to dynamic formatted segments

* separate the rendering and literal pools

* rsx output compiles again

* more examples compiling with new rsx expansion

* update template body explanation

* all workspace examples compile

* fix formatted segments in keys

* start hot reload diffing

* fix component literal hot reloading

* start integrate new hot reloading with the CLI

* simple hot reloads working

* Fix hot reloading blocks with components

* implement hot reloading for if chains

* Fix hot reloading after a template requires a full rebuild

* Fix hot reloading any attribute values

* remove unsafe from hot reload utils

* Fix hot reloading empty rsx

* add more hot reloading tests

* reorganize hot reload module

* fix web hydration

* fix empty rsx nodes in autoformatting

* fix tests

* remove path sorting logic from core

* make template names more consistent in debug mode

* fix quote_as_hot_reload_literal for explicitly typed literals

* fix can_be_shorthand for string literals

* fix formatted single dynamic expression

* Fix usize component properties and playwright tests

* remove default implementation for TemplateBody

* add a bunch more comments for diffing, scoring and why this scoring system is optimal
2024-07-30 17:16:27 +00:00
..
src Switch to a pool of dynamic values for hot reloading (#2705) 2024-07-30 17:16:27 +00:00
tests fix most typos, add crate-ci/typos to CI (#2653) 2024-07-23 17:49:33 -07:00
Cargo.toml switch to a Document trait and introduce Script/Head/Style/Meta components (#2635) 2024-07-17 20:54:03 -05:00
CHANGELOG.md feat: add changelogs 2022-01-29 10:17:14 -05:00
README.md Improve inline docs (#2460) 2024-06-06 18:15:17 -07:00

Dioxus Server-Side Rendering (SSR)

Render Dioxus to valid html.

Resources

This crate is a part of the broader Dioxus ecosystem. For more resources about Dioxus, check out:

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.

# 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>")

Basic Usage

The simplest example is to simply render some rsx! nodes to HTML. This can be done with the [render_element] API.

# use dioxus::prelude::*;
let content = dioxus_ssr::render_element(rsx!{
    div {
        for i in 0..5 {
            "Number: {i}"
        }
    }
});

Rendering a VirtualDom

# use dioxus::prelude::*;
# fn app() -> Element { todo!() }
let mut vdom = VirtualDom::new(app);
vdom.rebuild_in_place();

let content = dioxus_ssr::render(&vdom);

Usage in pre-rendering

This crate is particularly useful in pre-generating pages server-side and then selectively loading Dioxus client-side to pick up the reactive elements.

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.

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.

# use dioxus::prelude::*;
# fn App() -> Element { todo!() }
let mut vdom = VirtualDom::new(App);

vdom.rebuild_in_place();

let mut renderer = dioxus_ssr::Renderer::new();
renderer.pre_render = true;

let text = renderer.render(&vdom);

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.

# 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.

Usage in static site generation

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.