Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server-side Rendering (without hydration) (#2335)
* Basic render to html implementation. * Remove HtmlWriter. * Escape html content. * Add non-suspense tests. * Add Suspense tests. * Gated "ssr" feature. * Add example. * Fix tests. * Fix docs. * Fix heading size. * Remove the unused YewRenderer. * Remove extra comment. * unify naming. * Update docs. * Update docs. * Update docs. * Isolate spawn_local. * Add doc flags. * Add ssr feature to docs. * Move ServerRenderer into their own file. * Fix docs. * Update features and docs. * Fix example. * Adjust comment position. * Fix effects being wrongly called when a component is suspended. * Fix clippy. * Uuid & no double boxing. Co-authored-by: Muhammad Hamza <muhammadhamza1311@gmail.com>
- Loading branch information
Showing
30 changed files
with
1,450 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "simple_ssr" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
tokio = { version = "1.15.0", features = ["full"] } | ||
warp = "0.3" | ||
yew = { path = "../../packages/yew", features = ["ssr"] } | ||
reqwest = { version = "0.11.8", features = ["json"] } | ||
serde = { version = "1.0.132", features = ["derive"] } | ||
uuid = { version = "0.8.2", features = ["serde"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Server-side Rendering Example | ||
|
||
This example demonstrates server-side rendering. | ||
|
||
Run `cargo run -p simple_ssr` and navigate to http://localhost:8080/ to | ||
view results. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use tokio::task::LocalSet; | ||
use tokio::task::{spawn_blocking, spawn_local}; | ||
use uuid::Uuid; | ||
use warp::Filter; | ||
use yew::prelude::*; | ||
use yew::suspense::{Suspension, SuspensionResult}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct UuidResponse { | ||
uuid: Uuid, | ||
} | ||
|
||
async fn fetch_uuid() -> Uuid { | ||
// reqwest works for both non-wasm and wasm targets. | ||
let resp = reqwest::get("https://httpbin.org/uuid").await.unwrap(); | ||
let uuid_resp = resp.json::<UuidResponse>().await.unwrap(); | ||
|
||
uuid_resp.uuid | ||
} | ||
|
||
pub struct UuidState { | ||
s: Suspension, | ||
value: Rc<RefCell<Option<Uuid>>>, | ||
} | ||
|
||
impl UuidState { | ||
fn new() -> Self { | ||
let (s, handle) = Suspension::new(); | ||
let value: Rc<RefCell<Option<Uuid>>> = Rc::default(); | ||
|
||
{ | ||
let value = value.clone(); | ||
// we use tokio spawn local here. | ||
spawn_local(async move { | ||
let uuid = fetch_uuid().await; | ||
|
||
{ | ||
let mut value = value.borrow_mut(); | ||
*value = Some(uuid); | ||
} | ||
|
||
handle.resume(); | ||
}); | ||
} | ||
|
||
Self { s, value } | ||
} | ||
} | ||
|
||
impl PartialEq for UuidState { | ||
fn eq(&self, rhs: &Self) -> bool { | ||
self.s == rhs.s | ||
} | ||
} | ||
|
||
fn use_random_uuid() -> SuspensionResult<Uuid> { | ||
let s = use_state(UuidState::new); | ||
|
||
let result = match *s.value.borrow() { | ||
Some(ref m) => Ok(*m), | ||
None => Err(s.s.clone()), | ||
}; | ||
|
||
result | ||
} | ||
|
||
#[function_component] | ||
fn Content() -> HtmlResult { | ||
let uuid = use_random_uuid()?; | ||
|
||
Ok(html! { | ||
<div>{"Random UUID: "}{uuid}</div> | ||
}) | ||
} | ||
|
||
#[function_component] | ||
fn App() -> Html { | ||
let fallback = html! {<div>{"Loading..."}</div>}; | ||
|
||
html! { | ||
<Suspense {fallback}> | ||
<Content /> | ||
</Suspense> | ||
} | ||
} | ||
|
||
async fn render() -> String { | ||
let content = spawn_blocking(move || { | ||
use tokio::runtime::Builder; | ||
let set = LocalSet::new(); | ||
|
||
let rt = Builder::new_current_thread().enable_all().build().unwrap(); | ||
|
||
set.block_on(&rt, async { | ||
let renderer = yew::ServerRenderer::<App>::new(); | ||
|
||
renderer.render().await | ||
}) | ||
}) | ||
.await | ||
.expect("the thread has failed."); | ||
|
||
format!( | ||
r#"<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<title>Yew SSR Example</title> | ||
</head> | ||
<body> | ||
{} | ||
</body> | ||
</html> | ||
"#, | ||
content | ||
) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let routes = warp::any().then(|| async move { warp::reply::html(render().await) }); | ||
|
||
println!("You can view the website at: http://localhost:8080/"); | ||
|
||
warp::serve(routes).run(([127, 0, 0, 1], 8080)).await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
d8c2550
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yew master branch benchmarks (Lower is better)
yew-struct-keyed 01_run1k
246.7085
172.567
1.43
yew-struct-keyed 02_replace1k
265.42049999999995
192.423
1.38
yew-struct-keyed 03_update10th1k_x16
468.3485
421.674
1.11
yew-struct-keyed 04_select1k
79.83699999999999
86.3475
0.92
yew-struct-keyed 05_swap1k
99.389
99.2515
1.00
yew-struct-keyed 06_remove-one-1k
32.952
30.7395
1.07
yew-struct-keyed 07_create10k
2826.7855
2114.381
1.34
yew-struct-keyed 08_create1k-after1k_x2
556.9110000000001
407.3905
1.37
yew-struct-keyed 09_clear1k_x8
261.108
204.85
1.27
yew-struct-keyed 21_ready-memory
0.9196929931640624
0.9196929931640624
1
yew-struct-keyed 22_run-memory
1.4166336059570312
1.4500503540039062
0.98
yew-struct-keyed 23_update5-memory
1.4559898376464844
1.47119140625
0.99
yew-struct-keyed 24_run5-memory
1.476593017578125
1.4699134826660156
1.00
yew-struct-keyed 25_run-clear-memory
1.0852279663085938
1.0886917114257812
1.00
yew-struct-keyed 31_startup-ci
1730.022
1858.56725
0.93
yew-struct-keyed 32_startup-bt
39.25799999999998
29.76999999999999
1.32
yew-struct-keyed 34_startup-totalbytes
363.7998046875
363.7998046875
1
This comment was automatically generated by workflow using github-action-benchmark.