From ff4a0bab172e884b6965a3a56ef03adebcc08eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Tue, 21 Nov 2023 15:30:35 +0800 Subject: [PATCH] add yew-router suites for demo --- examples/wasi_ssr_module/Cargo.toml | 1 + examples/wasi_ssr_module/README.md | 2 +- examples/wasi_ssr_module/src/main.rs | 30 ++++++++++++++++++++++++-- examples/wasi_ssr_module/src/router.rs | 29 +++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 examples/wasi_ssr_module/src/router.rs diff --git a/examples/wasi_ssr_module/Cargo.toml b/examples/wasi_ssr_module/Cargo.toml index 7af8656b639..036c5bc5a3c 100644 --- a/examples/wasi_ssr_module/Cargo.toml +++ b/examples/wasi_ssr_module/Cargo.toml @@ -6,6 +6,7 @@ authors = ["langyo "] [dependencies] yew = { path = "../../packages/yew", features = ["ssr", "wasi"] } +yew-router = { path = "../../packages/yew-router", features = ["wasi"] } anyhow = "^1" bytes = "^1" diff --git a/examples/wasi_ssr_module/README.md b/examples/wasi_ssr_module/README.md index 58cacda16b2..5055c03163d 100644 --- a/examples/wasi_ssr_module/README.md +++ b/examples/wasi_ssr_module/README.md @@ -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. diff --git a/examples/wasi_ssr_module/src/main.rs b/examples/wasi_ssr_module/src/main.rs index f8e1397b78b..db23aa927a5 100644 --- a/examples/wasi_ssr_module/src/main.rs +++ b/examples/wasi_ssr_module/src/main.rs @@ -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! { + <> +

{"Yew WASI SSR demo"}

+ render={switch} /> + + } +} + #[function_component] fn App() -> Html { + use yew_router::{ + history::{AnyHistory, History, MemoryHistory}, + prelude::*, + }; + + let history = AnyHistory::from(MemoryHistory::new()); + history.push("/"); + html! {
- {"Hello, World!"} + + +
} } @@ -32,7 +59,6 @@ pub async fn render() -> Result { async fn main() -> Result<()> { let ret = render().await?; println!("{}", ret); - println!("{}", "Oh"); Ok(()) } diff --git a/examples/wasi_ssr_module/src/router.rs b/examples/wasi_ssr_module/src/router.rs new file mode 100644 index 00000000000..b37b582cec6 --- /dev/null +++ b/examples/wasi_ssr_module/src/router.rs @@ -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! {

{"Hello"}

} + } + Route::Thread { id } => { + html! {

{format!("Thread id {}", id)}

} + } + Route::NotFound => { + html! {

{"Not found"}

} + } + } +}