Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
65af3a2
Basic render to html implementation.
futursolo Jan 6, 2022
667796d
Remove HtmlWriter.
futursolo Jan 6, 2022
820d3ac
Escape html content.
futursolo Jan 6, 2022
1b2d823
Add non-suspense tests.
futursolo Jan 6, 2022
5b3f5e3
Add Suspense tests.
futursolo Jan 6, 2022
c7c9d2d
Gated "ssr" feature.
futursolo Jan 6, 2022
2bab219
Add example.
futursolo Jan 6, 2022
90deefc
merge master into ssr
futursolo Jan 6, 2022
482865e
Fix tests.
futursolo Jan 6, 2022
10f4ac4
Fix docs.
futursolo Jan 6, 2022
77b48c0
Fix heading size.
futursolo Jan 6, 2022
476300a
Remove the unused YewRenderer.
futursolo Jan 6, 2022
95e1d39
Remove extra comment.
futursolo Jan 6, 2022
655afd4
unify naming.
futursolo Jan 6, 2022
26bebb5
Update docs.
futursolo Jan 7, 2022
61f44d1
Update docs.
futursolo Jan 7, 2022
826b431
Update docs.
futursolo Jan 7, 2022
ed138b1
Isolate spawn_local.
futursolo Jan 7, 2022
bd26db4
Add doc flags.
futursolo Jan 7, 2022
f003aab
Add ssr feature to docs.
futursolo Jan 7, 2022
e56fcef
Move ServerRenderer into their own file.
futursolo Jan 7, 2022
ebe8c68
Fix docs.
futursolo Jan 7, 2022
10d7295
Update features and docs.
futursolo Jan 8, 2022
8635d63
Fix example.
futursolo Jan 8, 2022
3599240
Adjust comment position.
futursolo Jan 8, 2022
39a3d06
Fix effects being wrongly called when a component is suspended.
futursolo Jan 9, 2022
49566a0
Fix clippy.
futursolo Jan 9, 2022
17fd3b9
Uuid & no double boxing.
futursolo Jan 11, 2022
c67de39
merge master into ssr
futursolo Jan 11, 2022
0f24ae6
Merge branch 'master' into ssr
futursolo Jan 11, 2022
3567689
Merge branch 'master' into fursolo-ssr
ranile Jan 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/publish-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ jobs:
continue
fi

# ssr does not need trunk
if [[ "$example" == "simple_ssr" ]]; then
continue
fi

echo "building: $example"
(
cd "$path"
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ members = [
"examples/password_strength",
"examples/portals",
"examples/router",
"examples/simple_ssr",
"examples/timer",
"examples/todomvc",
"examples/two_apps",
Expand Down
7 changes: 6 additions & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ category = "Testing"
description = "Run all tests"
dependencies = ["tests-setup"]
env = { CARGO_MAKE_WORKSPACE_SKIP_MEMBERS = ["**/examples/*", "**/packages/changelog"] }
run_task = { name = ["test-flow", "doc-test-flow", "website-test"], fork = true }
run_task = { name = ["test-flow", "doc-test-flow", "ssr-test", "website-test"], fork = true }

[tasks.benchmarks]
category = "Testing"
Expand Down Expand Up @@ -117,3 +117,8 @@ category = "Maintainer processes"
toolchain = "stable"
command = "cargo"
args = ["run","-p","changelog", "--release", "${@}"]

[tasks.ssr-test]
env = { CARGO_MAKE_WORKSPACE_INCLUDE_MEMBERS = ["**/packages/yew"] }
private = true
workspace = true
2 changes: 1 addition & 1 deletion examples/futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0"
pulldown-cmark = { version = "0.9", default-features = false }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
yew = { path = "../../packages/yew" }
yew = { path = "../../packages/yew", features = ["tokio"] }
gloo-utils = "0.1"

[dependencies.web-sys]
Expand Down
14 changes: 14 additions & 0 deletions examples/simple_ssr/Cargo.toml
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"] }
6 changes: 6 additions & 0 deletions examples/simple_ssr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Server-side Rendering Example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to add your example in the table in examples/README.md
But don't add it now, I'm changing that table so ill just add it in my PR.


This example demonstrates server-side rendering.

Run `cargo run -p simple_ssr` and navigate to http://localhost:8080/ to
view results.
129 changes: 129 additions & 0 deletions examples/simple_ssr/src/main.rs
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;
}
2 changes: 1 addition & 1 deletion examples/suspense/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
yew = { path = "../../packages/yew" }
yew = { path = "../../packages/yew", features = ["tokio"] }
gloo-timers = { version = "0.2.2", features = ["futures"] }
wasm-bindgen-futures = "0.4"
wasm-bindgen = "0.2"
Expand Down
21 changes: 19 additions & 2 deletions packages/yew/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ indexmap = { version = "1", features = ["std"] }
js-sys = "0.3"
slab = "0.4"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
yew-macro = { version = "^0.19.0", path = "../yew-macro" }
thiserror = "1.0"

scoped-tls-hkt = "0.1"

futures = { version = "0.3", optional = true }
html-escape = { version = "0.2.9", optional = true }

[dependencies.web-sys]
version = "0.3"
features = [
Expand Down Expand Up @@ -61,17 +63,32 @@ features = [
"Window",
]

[target.'cfg(target_arch = "wasm32")'.dependencies]
# we move it here so no promise-based spawn_local can present for
# non-wasm32 targets.
wasm-bindgen-futures = "0.4"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.15.0", features = ["rt"], optional = true }

[dev-dependencies]
easybench-wasm = "0.2"
wasm-bindgen-test = "0.3"
gloo = { version = "0.6", features = ["futures"] }
wasm-bindgen-futures = "0.4"
rustversion = "1"
trybuild = "1"

[features]
doc_test = []
wasm_test = []
wasm_bench = []
ssr = ["futures", "html-escape"]
default = []

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.15.0", features = ["full"] }

[package.metadata.docs.rs]
features = ["doc_test"]
features = ["doc_test", "ssr"]
rustdoc-args = ["--cfg", "documenting"]
4 changes: 4 additions & 0 deletions packages/yew/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ args = [
"wasm_bench",
"bench",
]

[tasks.ssr-test]
command = "cargo"
args = ["test", "ssr_tests", "--features", "ssr"]
58 changes: 38 additions & 20 deletions packages/yew/src/functional/hooks/use_effect.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::functional::use_hook;
use std::{borrow::Borrow, rc::Rc};
use std::rc::Rc;

struct UseEffect<Destructor> {
runner: Option<Box<dyn FnOnce() -> Destructor>>,
destructor: Option<Box<Destructor>>,
}

Expand Down Expand Up @@ -39,20 +40,27 @@ pub fn use_effect<Destructor>(callback: impl FnOnce() -> Destructor + 'static)
where
Destructor: FnOnce() + 'static,
{
let callback = Box::new(callback);
use_hook(
move || {
let effect: UseEffect<Destructor> = UseEffect { destructor: None };
let effect: UseEffect<Destructor> = UseEffect {
runner: None,
destructor: None,
};
effect
},
|_, updater| {
|state, updater| {
state.runner = Some(Box::new(callback) as Box<dyn FnOnce() -> Destructor>);

// Run on every render
updater.post_render(move |state: &mut UseEffect<Destructor>| {
if let Some(de) = state.destructor.take() {
de();
if let Some(callback) = state.runner.take() {
if let Some(de) = state.destructor.take() {
de();
}

let new_destructor = callback();
state.destructor.replace(Box::new(new_destructor));
}
let new_destructor = callback();
state.destructor.replace(Box::new(new_destructor));
false
});
},
Expand All @@ -64,9 +72,15 @@ where
)
}

type UseEffectDepsRunnerFn<Dependents, Destructor> = Box<dyn FnOnce(&Dependents) -> Destructor>;

struct UseEffectDeps<Destructor, Dependents> {
runner_with_deps: Option<(
Rc<Dependents>,
UseEffectDepsRunnerFn<Dependents, Destructor>,
)>,
destructor: Option<Box<Destructor>>,
deps: Rc<Dependents>,
deps: Option<Rc<Dependents>>,
}

/// This hook is similar to [`use_effect`] but it accepts dependencies.
Expand All @@ -81,29 +95,33 @@ where
Dependents: PartialEq + 'static,
{
let deps = Rc::new(deps);
let deps_c = deps.clone();

use_hook(
move || {
let destructor: Option<Box<Destructor>> = None;
UseEffectDeps {
runner_with_deps: None,
destructor,
deps: deps_c,
deps: None,
}
},
move |_, updater| {
move |state, updater| {
state.runner_with_deps = Some((deps, Box::new(callback)));

updater.post_render(move |state: &mut UseEffectDeps<Destructor, Dependents>| {
if state.deps != deps {
if let Some((deps, callback)) = state.runner_with_deps.take() {
if Some(&deps) == state.deps.as_ref() {
return false;
}

if let Some(de) = state.destructor.take() {
de();
}
let new_destructor = callback(deps.borrow());
state.deps = deps;
state.destructor.replace(Box::new(new_destructor));
} else if state.destructor.is_none() {
state
.destructor
.replace(Box::new(callback(state.deps.borrow())));

let new_destructor = callback(&deps);

state.deps = Some(deps);
state.destructor = Some(Box::new(new_destructor));
}
false
});
Expand Down
Loading