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_router::{
|
||||
history::{AnyHistory, History, MemoryHistory},
|
||||
BrowserRouter, Router, Switch,
|
||||
history::{AnyHistory, MemoryHistory},
|
||||
BrowserRouter, Routable, Router, Switch,
|
||||
};
|
||||
|
||||
use crate::{components::layout::Layout, pages::Route};
|
||||
@ -71,14 +71,21 @@ impl HeadWriter {
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct StaticAppProps {
|
||||
pub url: String,
|
||||
pub route: Route,
|
||||
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)]
|
||||
pub fn static_app(props: &StaticAppProps) -> Html {
|
||||
let history = AnyHistory::from(MemoryHistory::default());
|
||||
history.push(&props.url);
|
||||
let history = props.create_history();
|
||||
|
||||
html! {
|
||||
<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 render = {
|
||||
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();
|
||||
@ -114,31 +114,31 @@ impl Env {
|
||||
}
|
||||
|
||||
struct RenderRoute {
|
||||
pub url: String,
|
||||
pub route: Route,
|
||||
pub path: PathBuf,
|
||||
}
|
||||
|
||||
fn collect_routes() -> Vec<RenderRoute> {
|
||||
enum_iterator::all::<Route>()
|
||||
.map(|route| {
|
||||
let url = route.to_path();
|
||||
let path = if url == "/" {
|
||||
let path = route.to_path();
|
||||
let path = if path == "/" {
|
||||
PathBuf::from("index.html")
|
||||
} else {
|
||||
PathBuf::from(&url[1..]).with_extension("html")
|
||||
PathBuf::from(&path[1..]).with_extension("html")
|
||||
};
|
||||
|
||||
RenderRoute { url, path }
|
||||
RenderRoute { route, path }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn render_routes(env: &Env) -> std::io::Result<()> {
|
||||
println!("Rendering routes ...");
|
||||
for RenderRoute { url, path } in collect_routes() {
|
||||
println!("Rendering route: {url}");
|
||||
for RenderRoute { route, path } in collect_routes() {
|
||||
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?;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user