[][src]Crate ssr


Add to your Cargo.toml:

ssr = "0.0.5"

And install node worker from npm:

// using npm
npm install --save ssr-rs

// or yarn
yarn add ssr-rs

How it works

On application start, you create an Ssr instance. Under the hood, it spins up a Node.js worker ready to accept rendering requests. Ssr instance should be stored in a web server's state, so handlers can access it during a handling of incoming requests.

Ssr exposes a single method render, which accepts Uri and serializable data as an input. If everything went smooth, it returns a rendered String. This string can be a plain HTML or an app-specific encoded object with additional metadata—whatever returned from a JS renderer, supplied by the app.


let ssr =
    SsrConfig {
      port: 9000,
      js_worker: PathBuf::from("./node_modules/ssr-rs/worker.js"),
      js_worker_log: JsWorkerLog::Verbose,
      global_js_renderer: Some(PathBuf::from("./js/ssr.js")),


A port that Node.js worker will be listening on.


Path to Node.js worker installed from npm. It should be relative to the std::env::current_dir.


Log verbosity of Node.js worker: either Minimal or Verbose.


If your web app is a SPA (Single Page Application), then you should have a single entry point for all rendering requests. If it's the case, provide a path to this file here and it will be used by the worker to render all responses. Another option is to provide a JS renderer per request but keep in mind that it would introduce additional runtime overhead since JS module has to be required during a request as opposed to requiring it once on application startup.


In request handlers, you need to get Ssr instance from your server's state. Once you have it (as well as all the required data to handle the current request), call ssr.render function with the following input:

  • Uri: uri of the current request
  • Data: impl Serialize: anything that implements Serialize
  • JsRenderer: an enum that tells to use either a global JS renderer or a renderer specific to this request.
let uri = req.uri();
let data = db::get_data();
match ssr.render(uri, &data, JsRenderer::Global).await {
    Ok(html) => HttpResponse::Ok().body(html),
    Err(error) => {
        error!("Error: {}", error);



The main struct of the crate that manages Node.js process and handles rendering.


A global configuration for Ssr instance.



Enum that instructs which JS renderer to use when ssr.render gets called.


Sets log verbosity of Node.js worker.