Skip to content

Server side rendering on rust servers using the v8 engine for parse and evaluate the javascript code.

License

Notifications You must be signed in to change notification settings

tbrockman/ssr-rs

 
 

Repository files navigation

🚀 Rust server side rendering

Valerioageno API codecov

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 v5.65.0; check it out here a full project who use this crate.

Getting started

Add this to your Cargo.toml:

[dependencies]
ssr_rs = "0.2.3"

Example

The all 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 js = Ssr::new(&source, "entryPoint");

    let html = js.render_to_string(None);
    
    assert_eq!(html, "<!doctype html><html>...</html>".to_string());
}

There are included examples with the most famous server frameworks and a default frontend react app made using npx create-react-app and the typescript --template flag. Check here the example ReactJS template.

Example with initial props

use ssr_rs::Ssr;
use std::fs::read_to_string;

fn main() {

    let props = r##"{
        "params": [
            "hello",
            "ciao",
            "こんにちは"
        ]
    }"##;

    let source = read_to_string("./path/to/build.js").unwrap();

    let js = Ssr::new(&source, "entryPoint");

    let html = js.render_to_string(Some(&props));

    assert_eq!(html, "<!doctype html><html>...</html>".to_string());
}

It's also possible just run the logic in a single shot just with Ssr::one_shot_render()

Example single shot

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::one_shot_render(Box::new(&source), "entryPoint", None);

    assert_eq!(html, "<!doctype html><hmtl>...</html>".to_string());
}

Contributing

Any helps or suggestions will be appreciated.

License

This project is licensed under the MIT License - see the LICENSE_MIT || LICENSE_APACHE file for more information.


About

Server side rendering on rust servers using the v8 engine for parse and evaluate the javascript code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%