Switch over to WebAssembly, Rust and Yew #35
17
src/app.rs
17
src/app.rs
@ -2,8 +2,8 @@ use std::sync::{Arc, Mutex};
|
|||||||
|
|
||||||
use yew::{function_component, html, ContextProvider, Html, Properties};
|
use yew::{function_component, html, ContextProvider, Html, Properties};
|
||||||
use yew_router::{
|
use yew_router::{
|
||||||
history::{AnyHistory, History, MemoryHistory},
|
history::{AnyHistory, MemoryHistory},
|
||||||
BrowserRouter, Router, Switch,
|
BrowserRouter, Routable, Router, Switch,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{components::layout::Layout, pages::Route};
|
use crate::{components::layout::Layout, pages::Route};
|
||||||
@ -71,14 +71,21 @@ impl HeadWriter {
|
|||||||
|
|
||||||
#[derive(Properties, PartialEq)]
|
#[derive(Properties, PartialEq)]
|
||||||
pub struct StaticAppProps {
|
pub struct StaticAppProps {
|
||||||
pub url: String,
|
pub route: Route,
|
||||||
pub head: HeadWriter,
|
pub head: HeadWriter,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl StaticAppProps {
|
||||||
|
fn create_history(&self) -> AnyHistory {
|
||||||
|
let path = self.route.to_path();
|
||||||
|
let history = MemoryHistory::with_entries(vec![path]);
|
||||||
|
history.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[function_component(StaticApp)]
|
#[function_component(StaticApp)]
|
||||||
pub fn static_app(props: &StaticAppProps) -> Html {
|
pub fn static_app(props: &StaticAppProps) -> Html {
|
||||||
let history = AnyHistory::from(MemoryHistory::default());
|
let history = props.create_history();
|
||||||
history.push(&props.url);
|
|
||||||
|
|
||||||
html! {
|
html! {
|
||||||
<ContextProvider<HeadWriter> context={props.head.clone()}>
|
<ContextProvider<HeadWriter> context={props.head.clone()}>
|
||||||
|
@ -80,12 +80,12 @@ impl Env {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn render_route(&self, url: String) -> String {
|
async fn render_route(&self, route: Route) -> String {
|
||||||
let head = HeadWriter::default();
|
let head = HeadWriter::default();
|
||||||
|
|
||||||
let render = {
|
let render = {
|
||||||
let head = head.clone();
|
let head = head.clone();
|
||||||
ServerRenderer::<StaticApp>::with_props(move || StaticAppProps { url, head })
|
ServerRenderer::<StaticApp>::with_props(move || StaticAppProps { route, head })
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut body = String::new();
|
let mut body = String::new();
|
||||||
@ -114,31 +114,31 @@ impl Env {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct RenderRoute {
|
struct RenderRoute {
|
||||||
pub url: String,
|
pub route: Route,
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_routes() -> Vec<RenderRoute> {
|
fn collect_routes() -> Vec<RenderRoute> {
|
||||||
enum_iterator::all::<Route>()
|
enum_iterator::all::<Route>()
|
||||||
.map(|route| {
|
.map(|route| {
|
||||||
let url = route.to_path();
|
let path = route.to_path();
|
||||||
let path = if url == "/" {
|
let path = if path == "/" {
|
||||||
PathBuf::from("index.html")
|
PathBuf::from("index.html")
|
||||||
} else {
|
} else {
|
||||||
PathBuf::from(&url[1..]).with_extension("html")
|
PathBuf::from(&path[1..]).with_extension("html")
|
||||||
};
|
};
|
||||||
|
|
||||||
RenderRoute { url, path }
|
RenderRoute { route, path }
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn render_routes(env: &Env) -> std::io::Result<()> {
|
async fn render_routes(env: &Env) -> std::io::Result<()> {
|
||||||
println!("Rendering routes ...");
|
println!("Rendering routes ...");
|
||||||
for RenderRoute { url, path } in collect_routes() {
|
for RenderRoute { route, path } in collect_routes() {
|
||||||
println!("Rendering route: {url}");
|
println!("Rendering route: {}", route.to_path());
|
||||||
|
|
||||||
let html = env.render_route(url).await;
|
let html = env.render_route(route).await;
|
||||||
env.write_str(path, &html).await?;
|
env.write_str(path, &html).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user