The project aims to enable server side rendering on rust servers in the simplest and lightest way possible.
It use an embedded version of the v8 javascript engine (rusty_v8) to parse and evaluate a built bundle file and return a string with the rendered html.
Currently it works with Webpack bundler v4.44.2; check it out here a full project who use this crate.
Gettin started
[]
= "0.2.1"
Example
The whole logic is stored inside the render_to_string() function.
use ssr_rs::Ssr;
use std::fs::read_to_string;
fn main() {
let source = read_to_string("./path/to/build.js").unwrap();
let html = Ssr::render_to_string(&source, "entryPoint", None);
assert_eq!(html, "<!doctype html>...".to_string());
}
Check how to use it with actix, rocket, warp and other frameworks here.
Example with initial props
use ssr_rs::Ssr;
use std::fs::read_to_string;
use serde_json;
fn main() {
let mock_props = r##"{
"params": [
"hello",
"ciao",
"こんにちは"
]
}"##;
let props = serde_json::to_string(&mock_props).unwrap();
let source = read_to_string("./path/to/build.js").unwrap();
let html = Ssr::render_to_string(&source, "entryPoint", Some(&props));
assert_eq!(html, "<!doctype html>...".to_string());
}