dioxus/packages/router/examples/static_generation.rs

107 lines
2.1 KiB
Rust
Raw Normal View History

2023-06-01 22:31:13 +00:00
#![allow(non_snake_case)]
use std::time::Duration;
2023-06-01 22:31:13 +00:00
use dioxus::prelude::*;
use dioxus_router::prelude::*;
2023-06-22 01:51:40 +00:00
2023-06-22 23:51:48 +00:00
use dioxus_ssr::incremental::{DefaultRenderer, IncrementalRendererConfig};
2023-06-01 22:31:13 +00:00
2023-06-24 20:44:22 +00:00
#[tokio::main]
async fn main() {
2023-07-26 17:28:49 +00:00
let mut renderer = IncrementalRendererConfig::new()
.static_dir("./static")
.invalidate_after(Duration::from_secs(10))
.build();
2023-06-01 22:31:13 +00:00
2023-06-26 23:08:53 +00:00
println!(
"SITE MAP:\n{}",
Route::SITE_MAP
.iter()
.flat_map(|route| route.flatten().into_iter())
.map(|route| {
route
.iter()
.map(|segment| segment.to_string())
.collect::<Vec<_>>()
.join("")
})
.collect::<Vec<_>>()
.join("\n")
);
2023-07-26 17:28:49 +00:00
pre_cache_static_routes::<Route, _>(
&mut renderer,
&DefaultRenderer {
before_body: r#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Dioxus Application</title>
</head>
<body>"#
.to_string(),
after_body: r#"</body>
</html>"#
.to_string(),
},
)
.await
.unwrap();
2023-06-01 22:31:13 +00:00
}
#[inline_props]
2023-06-22 01:36:32 +00:00
fn Blog(cx: Scope) -> Element {
2023-06-01 22:31:13 +00:00
render! {
2023-06-22 01:36:32 +00:00
div {
"Blog"
2023-06-01 22:31:13 +00:00
}
}
}
#[inline_props]
2023-06-22 01:36:32 +00:00
fn Post(cx: Scope, id: usize) -> Element {
2023-06-01 22:31:13 +00:00
render! {
div {
2023-06-22 01:36:32 +00:00
"PostId: {id}"
2023-06-01 22:31:13 +00:00
}
}
}
#[inline_props]
2023-06-22 01:36:32 +00:00
fn PostHome(cx: Scope) -> Element {
2023-06-01 22:31:13 +00:00
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 {},
2023-06-22 01:36:32 +00:00
#[route("/post/index")]
PostHome {},
#[route("/post/:id")]
Post {
id: usize,
},
2023-06-01 22:31:13 +00:00
#[end_nest]
#[route("/")]
Home {},
}