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

Open
wants to merge 91 commits into
base: master
Choose a base branch
from

Conversation

langyo
Copy link

@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
Copy link
Author

langyo commented Aug 10, 2024

cc rust-lang/rust#128618

Since the CI check failure issue mentioned earlier has been fixed in the newer compiler version (1.80.1), it can now be merged again.

I found that the benchmark CI of the master branch also had problems, and this problem was also merged here. I think this problem should be solved by another PR.

@ranile

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.

@langyo thanks for the ping, so sorry it look so long to get back to you and review the PR.

All the changes look good to me. Would you mind reverting the extra changes (reformat done by your editor)?

packages/yew/src/dom_bundle/btag/listeners.rs Outdated Show resolved Hide resolved
packages/yew/src/dom_bundle/subtree_root.rs Outdated Show resolved Hide resolved
Yew supports single thread mode for server-side rendering by `yew::LocalServerRenderer`. This mode would work in a single thread environment like WASI.

```rust
// Build it by `wasm32-wasi` target
Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. We can tell users that they can use networking from p2 if they want but not force it upon them

@langyo langyo requested a review from ranile August 13, 2024 15:32
Copy link
Member

@WorldSEnder WorldSEnder left a comment

Choose a reason for hiding this comment

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

Also sorry for taking a long time to review :) Apart from the comments from @ranile I'm in the process of figuring out why the workflows are failing or whether that's just some change with the chromedriver.

packages/yew-macro/src/html_tree/lint/mod.rs Outdated Show resolved Hide resolved
packages/yew-router/src/utils.rs Show resolved Hide resolved
packages/yew/Cargo.toml Outdated Show resolved Hide resolved
packages/yew/src/html/component/lifecycle.rs Outdated Show resolved Hide resolved
packages/yew/tests/raw_html.rs Show resolved Hide resolved
@langyo
Copy link
Author

langyo commented Aug 15, 2024

Although this commit is unlikely to support further improvements immediately, some small patches can be made.

I added a feature called not_browser_env to try to support serverless platforms such as Cloudflare Worker that can run wasm32-unknown-unknown target programs in NodeJS environment.

cc futursolo/stylist-rs#148 (comment)

Copy link
Member

@WorldSEnder WorldSEnder left a comment

Choose a reason for hiding this comment

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

Found a few more things, mostly about CI this time, but finally got a full overview of the whole PR so after this, I'm confident to merge.

.github/workflows/main-checks.yml Outdated Show resolved Hide resolved
.github/workflows/main-checks.yml Outdated Show resolved Hide resolved
Cargo.toml Show resolved Hide resolved
packages/yew/Cargo.toml Show resolved Hide resolved
.github/workflows/main-checks.yml Outdated Show resolved Hide resolved
website/docs/advanced-topics/server-side-rendering.md Outdated Show resolved Hide resolved
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