-
-
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
Merged
Merged
Changes from all 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 7bd1e04
Add async render for single-threaded env.
langyo a935141
Temporarily enable my own patch branch.
langyo 3338893
add example for WASI SSR.
langyo b218db9
Ready to run WASI on wasmtime.
langyo 7ef5659
complete the example
langyo dae7f15
fix fmt
langyo b2e0bdc
fix fmt
langyo 517ed00
I made a mistake..sry
langyo ff4a0ba
add yew-router suites for demo
langyo 1335d20
fix typo
langyo 1de83b7
Make the async render stream function public
langyo e91895a
Use target_os instead of feature.
langyo cc02f93
Renew gloo-history's patch.
langyo d9bae8d
Exclude WASI example to avoid web-sys.
langyo bf0c898
Try to add CI for WASI example.
langyo 0286b69
Fix CI.
langyo 666aa6a
Fix CI that requires compiler 1.67 or newer.
langyo 2c035e9
Use CLI's flag instead of exclude example.
langyo dea1dba
Remove patchs.
langyo 133169e
Use LocalServerRenderer instead of ServerRenderer.
langyo a520095
Remove unused exports.
langyo b3f2b6e
Add description about `LocalServerRenderer`.
langyo 459e110
fix fmt
langyo ec8ee8a
fix fmt
langyo 6699686
Update Cargo.lock
langyo 34b64ca
Bump rust compiler's version to 1.67...
langyo 44e1442
Exclude WASI on yew-router browser interfaces.
langyo d05e0d8
fix fmt
langyo 0b536c3
Wait for gloo's PR dealed.
langyo e40fbec
Rollback to rust compiler 1.64.
langyo 67293e2
Fix lock file.
langyo 1fc95e0
Downgrade `toml_datetime` version.
langyo 5a7be98
Fix enum for `gloo-history`.
langyo 6064d8b
Well, it seems there is no way to avoid the MSRV upgrade....
langyo 2712030
fix: Replace feature = "wasi" to target_os = "wasi".
langyo 7ed602d
Remove tips for rust version.
langyo 700731b
Bump `gloo` to 0.11.
langyo 73db848
Try to test yew-macro on compiler 1.67.
langyo 24515fd
Try to use compiler 1.68 instead.
langyo 56ca03c
Try to use compiler 1.69 instead......
langyo 326017c
Revert MSRV back
langyo 6349ad7
Pin the oldest Cargo.lock.
langyo fe37d55
Downgrade deps for MSRV.
langyo 9dee901
Bump benchmark tool's tokio to 1.35
langyo 5d3912b
Try to write WASI CI.
langyo 9045be2
Rollback the quotes
langyo 865469d
Combine CI files...
langyo de9b5f6
Rollback the use that gloo-history has fixed it.
langyo f0494f0
fix
langyo 0991e0a
Bump gloo-history version.
langyo 05650be
Block raw html update tests on WASI.
langyo edab060
Rollback indexmap's version.
langyo 1d8c171
fix CI
langyo 32e86d0
fix CI
langyo d79f9c2
Update some SSR test suites that replace ServerRender instead of Loca…
langyo 3eedd37
Remove yew-router's cfg macro
langyo da76e98
Fix fmt
langyo 6336f52
Try to fix CI
langyo c861fa5
Merge branch 'master' into wasi-support-test
langyo 1692b4a
Update examples/wasi_ssr_module/README.md
langyo 0b5bb9e
Revert back some unnecessary changes.
langyo e9429db
Clippy
langyo 8375313
fmt
langyo a7d1350
Merge branch 'yewstack:master' into wasi-support-test
langyo d148fcb
Fix CI.
langyo 491a2c1
Fix CI.
langyo d42077a
Try to fix clippy.
langyo f06042e
Merge commit '73d519e675277db92a7f892b946496c83e38c6d2' into wasi-sup…
langyo 6398986
Fix `ToString` trait.
langyo 2c6a150
Remove pin version of WASI CI test.
langyo 7a5e1f5
Pin the newer version.
langyo 321c99f
Fix typo.
langyo 47db4ba
Bump `wasm-bindgen`.
langyo 87d67f8
Fix SSR example.
langyo 30677e7
Fix typo.
langyo 1bbb3f7
Try to support non-browser environments.
langyo 4d5f6ed
Merge 1bbb3f73dc1f90a30d7e19282575e92b5434c8c2 into 73d519e675277db92…
langyo 4b69e36
Update wasm-bindgen-test to 0.3.43
WorldSEnder 0414af9
fix doc test running on nightly
WorldSEnder a6ece17
Update website/docs/advanced-topics/server-side-rendering.md
langyo 2df015a
Update WASI CI.
langyo 1d266cf
Remove WASI test for rustc 1.76.
langyo 768a914
Try to let `wasmtime` CLI can be executed.
langyo 9fdae79
Limit the function `decode_base64` that it shouldn't runnable in non-…
langyo d489d90
Remove WASI example test for rustc 1.76.
langyo cfc86bb
Revert changes.
langyo 4725390
Fix CI
langyo eb839a3
Merge branch 'master' into wasi-support-test
langyo ed4f96f
Fix Cargo.lock
langyo 4a3b79f
Remove unused deps
langyo 6be3606
Merge remote-tracking branch 'origin/master' into wasi-support-test
langyo 1290325
Undo the formatting changes.
langyo 4da0ba3
Merge branch 'master' into wasi-support-test
langyo cc486a4
Undo the formatting changes.
langyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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> |
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,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"] } |
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,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). |
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,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(()) | ||
} |
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,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> } | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![cfg(not(target_os = "wasi"))] | ||
|
||
use std::time::Duration; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![cfg(not(target_os = "wasi"))] | ||
|
||
use std::time::Duration; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![cfg(not(target_os = "wasi"))] | ||
|
||
use std::time::Duration; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![cfg(not(target_os = "wasi"))] | ||
|
||
use std::sync::atomic::{AtomicU8, Ordering}; | ||
use std::time::Duration; | ||
|
||
|
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![cfg(not(target_os = "wasi"))] | ||
|
||
use std::time::Duration; | ||
|
||
use yew::platform::time::sleep; | ||
|
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check 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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
This should be enough, no?
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.
Quite elegant..
I'll use it on the other SSR demos and delete this demo.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.
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 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