dioxus/packages/router/examples/static_generation.rs

88 lines
1.6 KiB
Rust
Raw Normal View History

2023-06-01 22:31:13 +00:00
#![allow(non_snake_case)]
use dioxus::prelude::*;
use dioxus_router::prelude::*;
2023-06-22 01:36:32 +00:00
use dioxus_router::ssr::{DefaultRenderer, IncrementalRendererConfig};
2023-06-01 22:31:13 +00:00
fn main() {
2023-06-22 01:36:32 +00:00
let mut renderer = IncrementalRendererConfig::new(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(),
})
.static_dir("./static")
.memory_cache_limit(5)
.build();
2023-06-01 22:31:13 +00:00
2023-06-22 01:36:32 +00:00
renderer.pre_cache_static::<Route>();
2023-06-01 22:31:13 +00:00
2023-06-22 01:36:32 +00:00
for _ in 0..2 {
for id in 0..10 {
renderer.render(Route::Post { id });
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 {},
}