- Notifications
You must be signed in to change notification settings - Fork13
Server side rendering on rust servers using the v8 engine for parse and evaluate the javascript code.
License
Apache-2.0, MIT licenses found
Licenses found
Valerioageno/ssr-rs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The crate aims to enable server side rendering on rust servers in the simplest and lightest way possible.
It uses an embedded version of theV8 javascript engine (rusty_v8) to parse and evaluate a built bundle file and return a string with the rendered html.
Note
This project is the backbone oftuono; a fullstack react framework with built in SSR.
Currently it works withVite,Webpack,Rspack,React 18 andSvelte 4 - Check theexamples/
folder.
Checkhere the benchmark results.
Add this to yourCargo.toml
:
cargo add ssr_rs
To render to string a bundled react project the application should perform the followingcalls.
use ssr_rs::Ssr;use std::fs::read_to_string;fnmain(){Ssr::create_platform();let source =read_to_string("./path/to/build.js").unwrap();letmut js =Ssr::new(&source,"entryPoint").unwrap();let html = js.render_to_string(None).unwrap();assert_eq!(html,"<!doctype html><html>...</html>".to_string());}
TheentryPoint
could be either:
- the function that returns an object with one or more properties that are functions that when called return the rendered result
- the object itself with one or more properties that are functions that when called return the rendered result
In case the bundled JS is an IIFE or the plain object theentryPoint
is an empty string.
// IIFE example | bundle.js -> See vite-react example(()=>({renderToStringFn:(props)=>"<html></html>"}))()// The entryPoint is an empty string
// Plain object example | bundle.js({renderToStringFn:(props)=>"<html></html>"});// The entryPoint is an empty string
// IIFE variable example | bundle.js -> See webpack-react examplevarSSR=(()=>({renderToStringFn:(props)=>"<html></html>"}))()// SSR is the entry point
// Variable example | bundle.js -> See webpack-react examplevarSSR={renderToStringFn:(props)=>"<html></html>"};// SSR is the entry point
The export results are managed by the bundler directly.
use ssr_rs::Ssr;use std::fs::read_to_string;fnmain(){Ssr::create_platform();let props =r##"{ "params": [ "hello", "ciao", "こんにちは" ] }"##;let source =read_to_string("./path/to/build.js").unwrap();letmut js =Ssr::new(&source,"entryPoint").unwrap();let html = js.render_to_string(Some(&props)).unwrap();assert_eq!(html,"<!doctype html><html>...</html>".to_string());}
Examples with different web frameworks are available in theexamples folder.
Even though the V8 engine allows accessing the sameisolate
from different threads that is forbidden by this crate for two reasons:
- rusty_v8 library have not implemented yet the V8 Locker API. Accessing Ssr struct from a different thread will make the V8 engine to panic.
- Rendering HTML does not need shared state across threads.
For the reasons above parallel computation is a better choice. Following actix-web setup:
use actix_web::{get, http::StatusCode,App,HttpResponse,HttpServer};use std::cell::RefCell;use std::fs::read_to_string;use ssr_rs::Ssr;thread_local!{staticSSR:RefCell<Ssr<'static,'static>> =RefCell::new(Ssr::from( read_to_string("./client/dist/ssr/index.js").unwrap(),"SSR").unwrap())}#[actix_web::main]asyncfnmain() -> std::io::Result<()>{Ssr::create_platform();HttpServer::new(||{App::new().service(index)}).bind("127.0.0.1:8080")?.run().await}#[get("/")]asyncfnindex() ->HttpResponse{let result =SSR.with(|ssr| ssr.borrow_mut().render_to_string(None).unwrap());HttpResponse::build(StatusCode::OK).content_type("text/html; charset=utf-8").body(result)}
Any helps or suggestions will be appreciated.
Known TODOs:
- Add examples with other rust backend frameworks
- Add examples with other frontend frameworks (i.e. vue, quik, solid)
- Add benchmark setup to test against Deno and Bun
- Explore support for V8 snapshots
- Explore js copilation to WASM (i.e.javy)
This project is licensed under the MIT License - see theLICENSE_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.