Skip to content
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

Merged
merged 95 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
263fc9d
Try to add wasi feature to avoid browser's ABI.
langyo Nov 14, 2023
7bd1e04
Add async render for single-threaded env.
langyo Nov 20, 2023
a935141
Temporarily enable my own patch branch.
langyo Nov 20, 2023
3338893
add example for WASI SSR.
langyo Nov 20, 2023
b218db9
Ready to run WASI on wasmtime.
langyo Nov 20, 2023
7ef5659
complete the example
langyo Nov 21, 2023
dae7f15
fix fmt
langyo Nov 21, 2023
b2e0bdc
fix fmt
langyo Nov 21, 2023
517ed00
I made a mistake..sry
langyo Nov 21, 2023
ff4a0ba
add yew-router suites for demo
langyo Nov 21, 2023
1335d20
fix typo
langyo Nov 21, 2023
1de83b7
Make the async render stream function public
langyo Nov 21, 2023
e91895a
Use target_os instead of feature.
langyo Nov 30, 2023
cc02f93
Renew gloo-history's patch.
langyo Dec 1, 2023
d9bae8d
Exclude WASI example to avoid web-sys.
langyo Dec 5, 2023
bf0c898
Try to add CI for WASI example.
langyo Dec 5, 2023
0286b69
Fix CI.
langyo Dec 5, 2023
666aa6a
Fix CI that requires compiler 1.67 or newer.
langyo Dec 5, 2023
2c035e9
Use CLI's flag instead of exclude example.
langyo Dec 6, 2023
dea1dba
Remove patchs.
langyo Dec 8, 2023
133169e
Use LocalServerRenderer instead of ServerRenderer.
langyo Dec 9, 2023
a520095
Remove unused exports.
langyo Dec 9, 2023
b3f2b6e
Add description about `LocalServerRenderer`.
langyo Dec 9, 2023
459e110
fix fmt
langyo Dec 9, 2023
ec8ee8a
fix fmt
langyo Dec 9, 2023
6699686
Update Cargo.lock
langyo Dec 9, 2023
34b64ca
Bump rust compiler's version to 1.67...
langyo Dec 9, 2023
44e1442
Exclude WASI on yew-router browser interfaces.
langyo Dec 9, 2023
d05e0d8
fix fmt
langyo Dec 9, 2023
0b536c3
Wait for gloo's PR dealed.
langyo Dec 9, 2023
e40fbec
Rollback to rust compiler 1.64.
langyo Dec 10, 2023
67293e2
Fix lock file.
langyo Dec 10, 2023
1fc95e0
Downgrade `toml_datetime` version.
langyo Dec 10, 2023
5a7be98
Fix enum for `gloo-history`.
langyo Dec 10, 2023
6064d8b
Well, it seems there is no way to avoid the MSRV upgrade....
langyo Dec 10, 2023
2712030
fix: Replace feature = "wasi" to target_os = "wasi".
langyo Dec 11, 2023
7ed602d
Remove tips for rust version.
langyo Dec 11, 2023
700731b
Bump `gloo` to 0.11.
langyo Dec 11, 2023
73db848
Try to test yew-macro on compiler 1.67.
langyo Dec 11, 2023
24515fd
Try to use compiler 1.68 instead.
langyo Dec 11, 2023
56ca03c
Try to use compiler 1.69 instead......
langyo Dec 11, 2023
326017c
Revert MSRV back
langyo Dec 11, 2023
6349ad7
Pin the oldest Cargo.lock.
langyo Dec 11, 2023
fe37d55
Downgrade deps for MSRV.
langyo Dec 11, 2023
9dee901
Bump benchmark tool's tokio to 1.35
langyo Dec 14, 2023
5d3912b
Try to write WASI CI.
langyo Dec 14, 2023
9045be2
Rollback the quotes
langyo Dec 15, 2023
865469d
Combine CI files...
langyo Dec 15, 2023
de9b5f6
Rollback the use that gloo-history has fixed it.
langyo Dec 15, 2023
f0494f0
fix
langyo Dec 15, 2023
0991e0a
Bump gloo-history version.
langyo Dec 15, 2023
05650be
Block raw html update tests on WASI.
langyo Dec 15, 2023
edab060
Rollback indexmap's version.
langyo Dec 15, 2023
1d8c171
fix CI
langyo Dec 15, 2023
32e86d0
fix CI
langyo Dec 15, 2023
d79f9c2
Update some SSR test suites that replace ServerRender instead of Loca…
langyo Dec 15, 2023
3eedd37
Remove yew-router's cfg macro
langyo Dec 15, 2023
da76e98
Fix fmt
langyo Dec 15, 2023
6336f52
Try to fix CI
langyo Dec 15, 2023
c861fa5
Merge branch 'master' into wasi-support-test
langyo Mar 19, 2024
1692b4a
Update examples/wasi_ssr_module/README.md
langyo Mar 28, 2024
0b5bb9e
Revert back some unnecessary changes.
langyo Mar 28, 2024
e9429db
Clippy
langyo Mar 28, 2024
8375313
fmt
langyo Mar 28, 2024
a7d1350
Merge branch 'yewstack:master' into wasi-support-test
langyo May 16, 2024
d148fcb
Fix CI.
langyo May 16, 2024
491a2c1
Fix CI.
langyo May 16, 2024
d42077a
Try to fix clippy.
langyo May 16, 2024
f06042e
Merge commit '73d519e675277db92a7f892b946496c83e38c6d2' into wasi-sup…
langyo Aug 10, 2024
6398986
Fix `ToString` trait.
langyo Aug 10, 2024
2c6a150
Remove pin version of WASI CI test.
langyo Aug 10, 2024
7a5e1f5
Pin the newer version.
langyo Aug 10, 2024
321c99f
Fix typo.
langyo Aug 13, 2024
47db4ba
Bump `wasm-bindgen`.
langyo Aug 14, 2024
87d67f8
Fix SSR example.
langyo Aug 14, 2024
30677e7
Fix typo.
langyo Aug 15, 2024
1bbb3f7
Try to support non-browser environments.
langyo Aug 15, 2024
4d5f6ed
Merge 1bbb3f73dc1f90a30d7e19282575e92b5434c8c2 into 73d519e675277db92…
langyo Aug 15, 2024
4b69e36
Update wasm-bindgen-test to 0.3.43
WorldSEnder Aug 21, 2024
0414af9
fix doc test running on nightly
WorldSEnder Aug 21, 2024
a6ece17
Update website/docs/advanced-topics/server-side-rendering.md
langyo Aug 22, 2024
2df015a
Update WASI CI.
langyo Aug 22, 2024
1d266cf
Remove WASI test for rustc 1.76.
langyo Aug 22, 2024
768a914
Try to let `wasmtime` CLI can be executed.
langyo Aug 22, 2024
9fdae79
Limit the function `decode_base64` that it shouldn't runnable in non-…
langyo Aug 22, 2024
d489d90
Remove WASI example test for rustc 1.76.
langyo Aug 22, 2024
cfc86bb
Revert changes.
langyo Aug 22, 2024
4725390
Fix CI
langyo Aug 22, 2024
eb839a3
Merge branch 'master' into wasi-support-test
langyo Aug 25, 2024
ed4f96f
Fix Cargo.lock
langyo Aug 25, 2024
4a3b79f
Remove unused deps
langyo Aug 26, 2024
6be3606
Merge remote-tracking branch 'origin/master' into wasi-support-test
langyo Oct 10, 2024
1290325
Undo the formatting changes.
langyo Oct 12, 2024
4da0ba3
Merge branch 'master' into wasi-support-test
langyo Oct 12, 2024
cc486a4
Undo the formatting changes.
langyo Oct 12, 2024
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: 4 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[target.'cfg(target_arch = "wasm32")']
[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))']
runner = 'wasm-bindgen-test-runner'

[target.'cfg(all(target_arch = "wasm32", target_os = "wasi"))']
runner = 'wasmtime -W unknown-imports-trap=y'

# This section needs to be last.
# GitHub Actions modifies this section.
[unstable]
Expand Down
79 changes: 74 additions & 5 deletions .github/workflows/main-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: Main Checks
on:
pull_request:
paths:
- ".github/workflows/main-checks.yml"
- "ci/**"
- "packages/**/*"
- "Cargo.toml"
- "Cargo.lock"
- '.github/workflows/main-checks.yml'
- 'ci/**'
- 'packages/**/*'
- 'Cargo.toml'
- 'Cargo.lock'
Copy link
Member

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

Copy link
Contributor Author

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

Oh..It's actually the automatic formatting by my VSCode. I'll change it back later.

Copy link
Member

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

push:
branches: [master]

Expand Down Expand Up @@ -170,3 +170,72 @@ jobs:
env:
RUSTFLAGS: --cfg nightly_yew --cfg yew_lints
run: cargo test -p yew-macro test_html_lints

unit_tests_wasi:
name: Unit Tests (WASI) on ${{ matrix.toolchain }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
toolchain:
- stable
- nightly

steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain }}
target: wasm32-wasip1

- name: Install wasmtime
run: |
wget https://github.com/bytecodealliance/wasmtime/releases/download/v24.0.0/wasmtime-v24.0.0-x86_64-linux.tar.xz
tar xf wasmtime-v24.0.0-x86_64-linux.tar.xz
mv wasmtime-v24.0.0-x86_64-linux/wasmtime ~/wasmtime
rm -rf wasmtime-v24.0.0-x86_64-linux.tar.xz wasmtime-v24.0.0-x86_64-linux
chmod +x ~/wasmtime
mv ~/wasmtime /usr/local/bin
source ~/.bashrc

- uses: Swatinem/rust-cache@v2

- name: Run WASI tests for yew
run: |
RUST_LOG=info
cargo test --features ssr,hydration --target wasm32-wasip1 -p yew

example-runnable-tests-on-wasi:
name: Example Runnable Tests on WASI
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
package:
- wasi_ssr_module
toolchain:
- stable
- nightly
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain }}
target: wasm32-wasip1

- name: Install wasmtime
run: |
wget https://github.com/bytecodealliance/wasmtime/releases/download/v24.0.0/wasmtime-v24.0.0-x86_64-linux.tar.xz
tar xf wasmtime-v24.0.0-x86_64-linux.tar.xz
mv wasmtime-v24.0.0-x86_64-linux/wasmtime ~/wasmtime
rm -rf wasmtime-v24.0.0-x86_64-linux.tar.xz wasmtime-v24.0.0-x86_64-linux
chmod +x ~/wasmtime
mv ~/wasmtime /usr/local/bin
source ~/.bashrc

- uses: Swatinem/rust-cache@v2

- name: Build and run ${{ matrix.package }}
run: |
cargo build --target wasm32-wasip1 -p ${{ matrix.package }}
wasmtime -W unknown-imports-trap=y target/wasm32-wasip1/debug/${{ matrix.package }}.wasm
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
members = [
"packages/*",
"tools/*",
"examples/*",
]
default-members = [
"packages/*",
"examples/*"
]
default-members = ["packages/*"]
langyo marked this conversation as resolved.
Show resolved Hide resolved
resolver = "2"

[profile.release]
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ As an example, check out the TodoMVC example here: <https://examples.yew.rs/todo
| [web_worker_fib](web_worker_fib) | [F] | Calculate Fibonacci numbers in a web worker thread using [`yew-agent`](https://docs.rs/yew-agent/latest/yew_agent/). |
| [web_worker_prime](web_worker_prime) | [F] | Calculate Prime numbers in a web worker thread using [`yew-agent`](https://docs.rs/yew-agent/latest/yew_agent/). |
| [webgl](webgl) | [S] | Controls a [WebGL canvas](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Getting_started_with_WebGL) from Yew. |
| [wasi_ssr_module](wasi_ssr_module) | [F] | Demonstrates server-side rendering using WASI. |

[CT]: ## "Component Type"
[S]: ## "Struct Components"
Expand Down
4 changes: 1 addition & 3 deletions examples/boids/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ gloo = "0.11"

[dependencies.web-sys]
version = "0.3"
features = [
"HtmlInputElement",
]
features = ["HtmlInputElement"]
4 changes: 1 addition & 3 deletions examples/function_memory_game/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ yew = { path = "../../packages/yew", features = ["csr"] }

[dependencies.web-sys]
version = "0.3"
features = [
"HtmlInputElement",
]
features = ["HtmlInputElement"]
4 changes: 1 addition & 3 deletions examples/function_todomvc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ yew = { path = "../../packages/yew", features = ["csr"] }

[dependencies.web-sys]
version = "0.3"
features = [
"HtmlInputElement",
]
features = ["HtmlInputElement"]
2 changes: 1 addition & 1 deletion examples/game_of_life/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/mount_point/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ features = [
"Element",
"HtmlCanvasElement",
"Node",
"Window"
"Window",
]
16 changes: 10 additions & 6 deletions examples/simple_ssr/index.html
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>
4 changes: 1 addition & 3 deletions examples/todomvc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ gloo = "0.11"

[dependencies.web-sys]
version = "0.3"
features = [
"HtmlInputElement",
]
features = ["HtmlInputElement"]
17 changes: 17 additions & 0 deletions examples/wasi_ssr_module/Cargo.toml
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"] }
23 changes: 23 additions & 0 deletions examples/wasi_ssr_module/README.md
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

@langyo langyo Dec 11, 2023

Choose a reason for hiding this comment

The 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?

It just specialize the entry that uses #[tokio::main(flavor = "current_thread")] to ensure tokio only provides one worker.

Copy link
Member

Choose a reason for hiding this comment

The 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() { ... }

Copy link
Contributor Author

@langyo langyo Dec 15, 2023

Choose a reason for hiding this comment

The 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() { ... }

Quite elegant..

I'll use it on the other SSR demos and delete this demo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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...

Copy link
Member

Choose a reason for hiding this comment

The 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).
60 changes: 60 additions & 0 deletions examples/wasi_ssr_module/src/main.rs
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(())
}
29 changes: 29 additions & 0 deletions examples/wasi_ssr_module/src/router.rs
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> }
}
}
}
2 changes: 1 addition & 1 deletion packages/yew-router-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full","extra-traits"] }
syn = { version = "2", features = ["full", "extra-traits"] }

[dev-dependencies]
rustversion = "1"
Expand Down
17 changes: 7 additions & 10 deletions packages/yew-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/yewstack/yew"
rust-version = "1.76.0"

[dependencies]
yew = { version = "0.21.0", path = "../yew", default-features= false }
yew = { version = "0.21.0", path = "../yew", default-features = false }
yew-router-macro = { version = "0.18.0", path = "../yew-router-macro" }

wasm-bindgen = "0.2"
Expand All @@ -26,19 +26,16 @@ urlencoding = "2.1.3"

[dependencies.web-sys]
version = "0.3"
features = [
"Document",
"HtmlBaseElement",
"Window",
]
features = ["Document", "HtmlBaseElement", "Window"]

[dev-dependencies]
wasm-bindgen-test = "0.3"
wasm-bindgen-test = "0.3.43"
serde = { version = "1", features = ["derive"] }
yew = { version = "0.21.0", path = "../yew", features = ["csr"] }

[dev-dependencies.web-sys]
version = "0.3"
features = [
"HtmlHeadElement",
]
features = ["HtmlHeadElement"]

[features]
default = []
Loading
Loading