finish router refrence
This commit is contained in:
1 parent
07446386e6
commit
b5a2f0d6cb
61 files changed
+676
-1908
No files matched your search
@@ -0,0 +1,115 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_router::prelude::*;
|
||||
use std::io::prelude::*;
|
||||
use std::{path::PathBuf, str::FromStr};
|
||||
|
||||
fn main() {
|
||||
render_static_pages();
|
||||
}
|
||||
|
||||
fn render_static_pages() {
|
||||
for route in Route::SITE_MAP
|
||||
.iter()
|
||||
.flat_map(|seg| seg.flatten().into_iter())
|
||||
{
|
||||
// check if this is a static segment
|
||||
let mut file_path = PathBuf::from("./");
|
||||
let mut full_path = String::new();
|
||||
let mut is_static = true;
|
||||
for segment in &route {
|
||||
match segment {
|
||||
SegmentType::Static(s) => {
|
||||
file_path.push(s);
|
||||
full_path += "/";
|
||||
full_path += s;
|
||||
}
|
||||
_ => {
|
||||
// skip routes with any dynamic segments
|
||||
is_static = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if is_static {
|
||||
let route = Route::from_str(&full_path).unwrap();
|
||||
let mut vdom = VirtualDom::new_with_props(RenderPath, RenderPathProps { path: route });
|
||||
let _ = vdom.rebuild();
|
||||
|
||||
file_path.push("index.html");
|
||||
std::fs::create_dir_all(file_path.parent().unwrap()).unwrap();
|
||||
let mut file = std::fs::File::create(file_path).unwrap();
|
||||
|
||||
let body = dioxus_ssr::render(&vdom);
|
||||
let html = format!(
|
||||
r#"
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{}</title>
|
||||
</head>
|
||||
<body>
|
||||
{}
|
||||
</body>
|
||||
</html>
|
||||
"#,
|
||||
full_path, body
|
||||
);
|
||||
file.write_all(html.as_bytes()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline_props]
|
||||
fn RenderPath(cx: Scope, path: Route) -> Element {
|
||||
let path = path.clone();
|
||||
render! {
|
||||
Router {
|
||||
config: || RouterConfig::default().history(MemoryHistory::with_initial_path(path))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline_props]
|
||||
fn Blog(cx: Scope) -> Element {
|
||||
render! {
|
||||
div {
|
||||
"Blog"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline_props]
|
||||
fn Post(cx: Scope) -> Element {
|
||||
render! {
|
||||
div {
|
||||
"Post"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline_props]
|
||||
fn Home(cx: Scope) -> Element {
|
||||
render! {
|
||||
div {
|
||||
"Home"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[derive(Clone, Debug, PartialEq, Routable)]
|
||||
enum Route {
|
||||
#[nest("/blog")]
|
||||
#[route("/")]
|
||||
Blog {},
|
||||
#[route("/post")]
|
||||
Post {},
|
||||
#[end_nest]
|
||||
#[route("/")]
|
||||
Home {},
|
||||
}
|
||||
@@ -7,7 +7,7 @@ pub mod navigation;
|
||||
pub mod routable;
|
||||
|
||||
/// Components interacting with the router.
|
||||
mod components {
|
||||
pub mod components {
|
||||
mod default_errors;
|
||||
pub use default_errors::*;
|
||||
|
||||
@@ -37,7 +37,7 @@ mod router_cfg;
|
||||
mod history;
|
||||
|
||||
/// Hooks for interacting with the router in components.
|
||||
mod hooks {
|
||||
pub mod hooks {
|
||||
mod use_router;
|
||||
pub(crate) use use_router::*;
|
||||
|
||||
|
||||
Reference in new issue
Block a user