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

Conversation

langyo
Copy link
Contributor

@langyo langyo commented Nov 21, 2023

Description

I've been trying to use yew to render the page into the static HTML string on WASI. However, yew cannot distinguish the browser WASM target (wasm32-unknown-unknown with wasm-bindgen) and WASI target (wasm32-wasi), and it would choose wrong modules for wasm32-*.

In addition, since the current SSR implementation will create new tasks directly in the asynchronous context directly (based on prokio). It only allowed in a multi-threaded environment that it is not compatible with WASI. So I added a dedicated one for a single-threaded environment that rendering function to support single-threaded scenes.

To support this new feature, I have also made changes along with some other upstream dependencies. I've created some PRs for these upstream dependencies, and added a temporary patch entry to the root Cargo.toml for this PR. If the upstream branch can be processed, these temporary patch entries could be replaced.

I also wrote a short example for this new feature.

CC @futursolo 😉

Checklist

  • I have reviewed my own code
  • I have added tests

Copy link

github-actions bot commented Nov 21, 2023

Benchmark - core

Yew Master

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.968 ns      │ 3.041 ns      │ 2.988 ns      │ 2.989 ns      │ 100     │ 1000000000

Pull Request

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.986 ns      │ 3.096 ns      │ 2.993 ns      │ 2.995 ns      │ 100     │ 1000000000

Copy link

github-actions bot commented Nov 21, 2023

Visit the preview URL for this PR (updated for commit 4a3b79f):

https://yew-rs--pr3534-wasi-support-test-8w8rolp5.web.app

(expires Mon, 02 Sep 2024 02:55:49 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Copy link

github-actions bot commented Nov 21, 2023

Benchmark - SSR

Yew Master

Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 289.303 289.917 289.487 0.244
Hello World 10 495.218 502.854 498.844 3.225
Function Router 10 1655.731 1690.352 1672.628 12.751
Concurrent Task 10 1005.178 1006.620 1005.978 0.494
Many Providers 10 1122.085 1162.368 1138.665 13.104

Pull Request

Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 289.249 289.463 289.345 0.073
Hello World 10 515.033 518.411 516.701 1.213
Function Router 10 1614.825 1653.319 1623.054 11.098
Concurrent Task 10 1004.953 1006.618 1005.731 0.469
Many Providers 10 1183.398 1218.504 1193.656 12.951

Copy link

github-actions bot commented Nov 21, 2023

Size Comparison

examples master (KB) pull request (KB) diff (KB) diff (%)
async_clock 96.327 96.327 0 0.000%
boids 168.066 168.066 0 0.000%
communication_child_to_parent 89.818 89.818 0 0.000%
communication_grandchild_with_grandparent 101.845 101.845 0 0.000%
communication_grandparent_to_grandchild 97.510 97.510 0 0.000%
communication_parent_to_child 86.358 86.358 0 0.000%
contexts 101.714 101.714 0 0.000%
counter 82.761 82.761 0 0.000%
counter_functional 83.209 83.209 0 0.000%
dyn_create_destroy_apps 85.966 85.966 0 0.000%
file_upload 95.986 95.986 0 0.000%
function_memory_game 167.535 167.535 0 0.000%
function_router 333.431 333.411 -0.020 -0.006%
function_todomvc 160.954 160.954 0 0.000%
futures 234.979 234.979 0 0.000%
game_of_life 104.846 104.846 0 0.000%
immutable 185.640 185.640 0 0.000%
inner_html 77.420 77.420 0 0.000%
js_callback 104.540 104.540 0 0.000%
keyed_list 195.628 195.628 0 0.000%
mount_point 80.155 80.155 0 0.000%
nested_list 109.956 109.956 0 0.000%
node_refs 87.709 87.709 0 0.000%
password_strength 1777.415 1777.415 0 0.000%
portals 90.337 90.337 0 0.000%
router 304.342 304.325 -0.017 -0.005%
simple_ssr 137.193 137.193 0 0.000%
ssr_router 370.538 370.515 -0.023 -0.006%
suspense 108.910 108.910 0 0.000%
timer 85.505 85.505 0 0.000%
timer_functional 93.200 93.200 0 0.000%
todomvc 141.917 141.917 0 0.000%
two_apps 82.834 82.834 0 0.000%
web_worker_fib 131.337 131.337 0 0.000%
web_worker_prime 182.851 182.851 0 0.000%
webgl 80.139 80.139 0 0.000%

✅ None of the examples has changed their size significantly.

@langyo langyo requested a review from WorldSEnder August 22, 2024 07:58
@langyo langyo force-pushed the wasi-support-test branch from 5cc0207 to 4a3b79f Compare October 10, 2024 13:44
ranile
ranile previously approved these changes Oct 10, 2024
Copy link
Member

@ranile ranile left a comment

Choose a reason for hiding this comment

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

Thanks so much for your patience. My apologies for how long it took to review

@ranile
Copy link
Member

ranile commented Oct 12, 2024

@WorldSEnder this is pending your review. If you're unable to review, I'll dismiss the previous (now-resolved) review and get this merged

@ranile
Copy link
Member

ranile commented Oct 12, 2024

@langyo can you please undo the formatting changes your editor did in the meantime? I'll merge this PR on Monday if @WorldSEnder doesn't respond by then

@langyo langyo force-pushed the wasi-support-test branch 3 times, most recently from 8d07370 to 6155846 Compare October 12, 2024 16:54
@langyo langyo force-pushed the wasi-support-test branch from 6155846 to cc486a4 Compare October 12, 2024 16:55
@langyo langyo requested a review from ranile October 12, 2024 17:12
@ranile ranile dismissed WorldSEnder’s stale review October 16, 2024 12:22

Review already addressed, need to dismiss to merge PR

@ranile ranile merged commit 84b7548 into yewstack:master Oct 21, 2024
26 checks passed
@ranile
Copy link
Member

ranile commented Oct 21, 2024

Merging this (finally!). Any changes needed can come in further PRs

geoffjay pushed a commit to geoffjay/yew that referenced this pull request Jan 25, 2025
* Try to add wasi feature to avoid browser's ABI.

* Add async render for single-threaded env.

* Temporarily enable my own patch branch.
It would be modified later
after the corresponding library branches are merged.

* add example for WASI SSR.

* Ready to run WASI on wasmtime.

* complete the example

* fix fmt

* fix fmt

* I made a mistake..sry

* add yew-router suites for demo

* fix typo

* Make the async render stream function public

* Use target_os instead of feature.

* Renew gloo-history's patch.

* Exclude WASI example to avoid web-sys.

* Try to add CI for WASI example.

* Fix CI.

* Fix CI that requires compiler 1.67 or newer.

* Use CLI's flag instead of exclude example.
bytecodealliance/wasmtime#4312

* Remove patchs.

* Use LocalServerRenderer instead of ServerRenderer.
yewstack/tokise#11 (comment)

* Remove unused exports.

* Add description about `LocalServerRenderer`.

* fix fmt

* fix fmt

* Update Cargo.lock

* Bump rust compiler's version to 1.67...

* Exclude WASI on yew-router browser interfaces.

* fix fmt

* Wait for gloo's PR dealed.

* Rollback to rust compiler 1.64.
cc rustwasm/gloo#423 (comment)

* Fix lock file.

* Downgrade `toml_datetime` version.

* Fix enum for `gloo-history`.

* Well, it seems there is no way to avoid the MSRV upgrade....

* fix: Replace feature = "wasi" to target_os = "wasi".

* Remove tips for rust version.

* Bump `gloo` to 0.11.

* Try to test yew-macro on compiler 1.67.

* Try to use compiler 1.68 instead.

* Try to use compiler 1.69 instead......

* Revert MSRV back

* Pin the oldest Cargo.lock.

* Downgrade deps for MSRV.

* Bump benchmark tool's tokio to 1.35

* Try to write WASI CI.

* Rollback the quotes

* Combine CI files...

* Rollback the use that gloo-history has fixed it.

* fix

* Bump gloo-history version.

* Block raw html update tests on WASI.

* Rollback indexmap's version.

* fix CI

* fix CI

* Update some SSR test suites that replace ServerRender instead of LocalServerRender.

* Remove yew-router's cfg macro

* Fix fmt

* Try to fix CI

* Update examples/wasi_ssr_module/README.md

Co-authored-by: Elina <imelina@elina.website>

* Revert back some unnecessary changes.

* Clippy

* fmt

* Fix CI.

* Fix CI.

* Try to fix clippy.

* Fix `ToString` trait.

* Remove pin version of WASI CI test.

* Pin the newer version.

* Fix typo.

* Bump `wasm-bindgen`.

* Fix SSR example.

* Fix typo.

* Try to support non-browser environments.

* Update wasm-bindgen-test to 0.3.43

refer to rustwasm/wasm-bindgen#4083

* fix doc test running on nightly

* Update website/docs/advanced-topics/server-side-rendering.md

Co-authored-by: WorldSEnder <WorldSEnder@users.noreply.github.com>

* Update WASI CI.

* Remove WASI test for rustc 1.76.

* Try to let `wasmtime` CLI can be executed.

* Limit the function `decode_base64` that it shouldn't runnable in non-browser environment.

* Remove WASI example test for rustc 1.76.

* Revert changes.

* Fix CI

* Fix Cargo.lock

* Remove unused deps

* Undo the formatting changes.

* Undo the formatting changes.

---------

Co-authored-by: Elina <imelina@elina.website>
Co-authored-by: Martin Molzer <WorldSEnder@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants