Skip to content

Commit

Permalink
add yew-router suites for demo
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Nov 21, 2023
1 parent 517ed00 commit ff4a0ba
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/wasi_ssr_module/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ authors = ["langyo <[email protected]>"]

[dependencies]
yew = { path = "../../packages/yew", features = ["ssr", "wasi"] }
yew-router = { path = "../../packages/yew-router", features = ["wasi"] }

anyhow = "^1"
bytes = "^1"
Expand Down
2 changes: 1 addition & 1 deletion examples/wasi_ssr_module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ cargo build --package wasi_ssr_module --target wasm32-wasi --release
wasmtime target/wasm32-wasi/release/wasi_ssr_module.wasm
```

> Warn: This example is not yet fully functional. For some unknown reason, this demo only works [outside this project](https://github.com/celestia-island/tairitsu/blob/e032b536984e449e14941c9b755d747a0aa366fb/packages/proto/src/html/render.rs) because the dependency `web-sys` includes some objects forcible. It would have crashed when running on this project that caused by unknown import `__wbindgen_placeholder__::__wbindgen_xxx` has not been defined in the WASI environment.
> Warn: This example is not yet fully functional. For some unknown reason, this demo only works [outside this project](https://github.com/celestia-island/tairitsu/blob/a724e3f34754fadf279f036e2c473cbf2abf4b8b/packages/proto/src/html/render.rs) because the dependency `web-sys` includes some objects forcible. It would have crashed when running on this project that caused by unknown import `__wbindgen_placeholder__::__wbindgen_xxx` has not been defined in the WASI environment.
30 changes: 28 additions & 2 deletions examples/wasi_ssr_module/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
#![allow(unused_imports)]
#![allow(non_snake_case)]

mod router;

use anyhow::Result;

use yew::prelude::*;
use yew::{function_component, ServerRenderer};

use router::{switch, Route};

#[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},
prelude::*,
};

let history = AnyHistory::from(MemoryHistory::new());
history.push("/");

html! {
<div>
{"Hello, World!"}
<Router history={history}>
<Content />
</Router>
</div>
}
}
Expand All @@ -32,7 +59,6 @@ pub async fn render() -> Result<String> {
async fn main() -> Result<()> {
let ret = render().await?;
println!("{}", ret);
println!("{}", "Oh");

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> }
}
}
}

0 comments on commit ff4a0ba

Please sign in to comment.