-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add WASI support for server-side rendering. #3534
Changes from 92 commits
263fc9d
7bd1e04
a935141
3338893
b218db9
7ef5659
dae7f15
b2e0bdc
517ed00
ff4a0ba
1335d20
1de83b7
e91895a
cc02f93
d9bae8d
bf0c898
0286b69
666aa6a
2c035e9
dea1dba
133169e
a520095
b3f2b6e
459e110
ec8ee8a
6699686
34b64ca
44e1442
d05e0d8
0b536c3
e40fbec
67293e2
1fc95e0
5a7be98
6064d8b
2712030
7ed602d
700731b
73db848
24515fd
56ca03c
326017c
6349ad7
fe37d55
9dee901
5d3912b
9045be2
865469d
de9b5f6
f0494f0
0991e0a
05650be
edab060
1d8c171
32e86d0
d79f9c2
3eedd37
da76e98
6336f52
c861fa5
1692b4a
0b5bb9e
e9429db
8375313
a7d1350
d148fcb
491a2c1
d42077a
f06042e
6398986
2c6a150
7a5e1f5
321c99f
47db4ba
87d67f8
30677e7
1bbb3f7
4d5f6ed
4b69e36
0414af9
a6ece17
2df015a
1d266cf
768a914
9fdae79
d489d90
cfc86bb
4725390
eb839a3
ed4f96f
4a3b79f
6be3606
1290325
4da0ba3
cc486a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ version = "0.1.4" | |
authors = [ | ||
"Diego Cardoso <[email protected]>", | ||
"Ilya Bogdanov <[email protected]", | ||
"Junjie Huang <[email protected]>" | ||
"Junjie Huang <[email protected]>", | ||
] | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,5 @@ features = [ | |
"Element", | ||
"HtmlCanvasElement", | ||
"Node", | ||
"Window" | ||
"Window", | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Yew SSR Example</title> | ||
|
||
<link data-trunk rel="rust" data-bin="simple_ssr_hydrate" data-cargo-features="hydration" /> | ||
</head> | ||
</html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Yew SSR Example</title> | ||
|
||
<link data-trunk rel="rust" data-bin="simple_ssr_hydrate" data-cargo-features="hydration" /> | ||
</head> | ||
|
||
<body></body> | ||
|
||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "wasi_ssr_module" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["langyo <[email protected]>"] | ||
|
||
[dependencies] | ||
yew = { path = "../../packages/yew", features = ["ssr"] } | ||
yew-router = { path = "../../packages/yew-router" } | ||
|
||
anyhow = "^1" | ||
bytes = "^1" | ||
serde = { version = "^1", features = ["derive"] } | ||
serde_json = "^1" | ||
lazy_static = "^1" | ||
|
||
tokio = { version = "^1", features = ["macros", "rt", "time"] } |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this example any different from other router ones when it comes to running in WASI? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It just specialize the entry that uses #[tokio::main(flavor = "current_thread")] to ensure tokio only provides one worker. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be enough, no? #[cfg_attr(target_os = "wasi", tokio::main(flavor = "current_thread"))]
#[cfg_attr(not(target_os = "wasi"), tokio::main)]
fn main() { ... } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Quite elegant..
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But there's still a problem. The WASI environment cannot directly establish a server like other environments. Because this part has not been standardized yet. Originally I wanted to remove this demo, but I discovered this problem when checking other demos... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought preview2 added support for it, no? I don't mind forcing WASI consumers to use wasm32-wasip2 target |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# WASI SSR Module Example | ||
|
||
This example demonstrates how to use the WASI target to run a simple server-side rendering application. | ||
|
||
It depends on [wasmtime](https://wasmtime.dev)'s WASI preview2. | ||
|
||
## Building | ||
|
||
To build the example, run the following command from the root of the repository: | ||
|
||
```bash | ||
cargo build --manifest-path examples/wasi_ssr_module/Cargo.toml --target wasm32-wasi --release | ||
``` | ||
|
||
## Running | ||
|
||
> Note: This example requires the wasmtime CLI to be installed. See [wasmtime's installation instructions](https://docs.wasmtime.dev/cli-install.html) for more information. | ||
|
||
```bash | ||
wasmtime target/wasm32-wasi/release/wasi_ssr_module.wasm | ||
``` | ||
|
||
> Note: If your wasmtime CLI throws an error that it says some imports like `__wbindgen_placeholder__::__wbindgen_xxx` is invalid, try to run `cargo update`. See issue [rustwasm/gloo#411](https://github.com/rustwasm/gloo/pull/411#discussion_r1421219033). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#![allow(unused_imports)] | ||
#![allow(non_snake_case)] | ||
|
||
mod router; | ||
|
||
use anyhow::Result; | ||
use router::{switch, Route}; | ||
use yew::prelude::*; | ||
use yew::LocalServerRenderer; | ||
|
||
#[function_component] | ||
fn Content() -> Html { | ||
use yew_router::prelude::*; | ||
|
||
html! { | ||
<> | ||
<h1>{"Yew WASI SSR demo"}</h1> | ||
<Switch<Route> render={switch} /> | ||
</> | ||
} | ||
} | ||
|
||
#[function_component] | ||
fn App() -> Html { | ||
use yew_router::history::{AnyHistory, History, MemoryHistory}; | ||
use yew_router::prelude::*; | ||
|
||
let history = AnyHistory::from(MemoryHistory::new()); | ||
history.push("/"); | ||
|
||
html! { | ||
<div> | ||
<Router history={history}> | ||
<Content /> | ||
</Router> | ||
</div> | ||
} | ||
} | ||
|
||
pub async fn render() -> Result<String> { | ||
let renderer = LocalServerRenderer::<App>::new(); | ||
let html_raw = renderer.render().await; | ||
|
||
let mut body = String::new(); | ||
body.push_str("<body>"); | ||
body.push_str("<div id='app' style='width: 100vw; height: 100vh; position: fixed;'>"); | ||
body.push_str(&html_raw); | ||
body.push_str("</div>"); | ||
body.push_str("</body>"); | ||
|
||
Ok(body) | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<()> { | ||
let ret = render().await?; | ||
println!("{}", ret); | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use yew::prelude::*; | ||
use yew_router::prelude::*; | ||
|
||
#[derive(Routable, PartialEq, Eq, Clone, Debug)] | ||
pub enum Route { | ||
#[at("/")] | ||
Portal, | ||
|
||
#[at("/t/:id")] | ||
Thread { id: String }, | ||
|
||
#[not_found] | ||
#[at("/404")] | ||
NotFound, | ||
} | ||
|
||
pub fn switch(routes: Route) -> Html { | ||
match routes { | ||
Route::Portal => { | ||
html! { <h1>{"Hello"}</h1> } | ||
} | ||
Route::Thread { id } => { | ||
html! { <h1>{format!("Thread id {}", id)}</h1> } | ||
} | ||
Route::NotFound => { | ||
html! { <h1>{"Not found"}</h1> } | ||
} | ||
} | ||
} |
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.
Nit: it would be better if you didn't change the formatting in this PR
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.
Oh..It's actually the automatic formatting by my VSCode. I'll change it back later.
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.
This still needs to be resolved. Lots of unnecessary changes here